0

这是我第一次测试颤振应用程序。我试图HomeView用这个测试代码来抽我:

void main() {
  group('Home Test', () {
    _pumpHome(WidgetTester tester) => tester.pumpWidget(
          MaterialApp(
            home: HomeView(),
          ),
        );

    testWidgets('Route to Azkar page', (WidgetTester tester) async {
      await _pumpHome(tester);
      await tester.tap(find.byKey(Key('morning')));
      expect(find.byType(ListView), findsOneWidget);
    });
  });
}

但这两个例外发生了。

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following LateError was thrown building HomeView(dirty):
LateInitializationError: Field '_instance@99075166' has not been initialized.

The relevant error-causing widget was:
  HomeView file:///E:/projects/flutterProject/tafra/test/home_test.dart:26:19

When the exception was thrown, this was the stack:
#0      ScreenUtil._instance (package:flutter_screenutil/screen_util.dart)
#1      new ScreenUtil (package:flutter_screenutil/screen_util.dart:28:12)
#2      SizeExtension.w (package:flutter_screenutil/size_extension.dart:5:19)
#3      HomeView.build (package:tafra/app/modules/home/views/home_view.dart:23:27)
#4      StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5)
#8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
...     Normal element mounting (166 frames)
#174    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#175    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#176    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
...     Normal element mounting (354 frames)
#530    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#531    Element.updateChild (package:flutter/src/widgets/framework.dart:3360:20)
#532    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1189:16)
#533    RenderObjectToWidgetElement.update (package:flutter/src/widgets/binding.dart:1167:5)
#534    RenderObjectToWidgetElement.performRebuild (package:flutter/src/widgets/binding.dart:1181:7)
#535    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#536    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2582:33)
#537    AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1106:19)
#538    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5)
#539    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#540    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#541    AutomatedTestWidgetsFlutterBinding.pump.<anonymous closure> (package:flutter_test/src/binding.dart:973:9)
#544    TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#545    AutomatedTestWidgetsFlutterBinding.pump (package:flutter_test/src/binding.dart:960:27)
#546    WidgetTester.pumpWidget.<anonymous closure> (package:flutter_test/src/widget_tester.dart:534:22)
#549    TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#550    WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:531:27)
#551    main.<anonymous closure>._pumpHome (file:///E:/projects/flutterProject/tafra/test/home_test.dart:19:46)
#552    main.<anonymous closure>.<anonymous closure> (file:///E:/projects/flutterProject/tafra/test/home_test.dart:31:22)
#553    main.<anonymous closure>.<anonymous closure> (file:///E:/projects/flutterProject/tafra/test/home_test.dart:30:40)
#554    testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:156:29)
<asynchronous suspension>
<asynchronous suspension>
(elided 5 frames from dart:async and package:stack_trace)

═══════════════════════════════════════════════════════════════════════════════════════════════════╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
The finder "zero widgets with key [<'morning'>] (ignoring offstage widgets)" (used in a call to
"tap()") could not find any matching widgets.

When the exception was thrown, this was the stack:
#0      WidgetController._getElementPoint (package:flutter_test/src/controller.dart:897:7)
#1      WidgetController.getCenter (package:flutter_test/src/controller.dart:836:12)
#2      WidgetController.tap (package:flutter_test/src/controller.dart:271:18)
#3      main.<anonymous closure>.<anonymous closure> (file:///E:/projects/flutterProject/tafra/test/home_test.dart:32:20)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

我认为第二个例外的原因是第一个。

我的HomeView生产代码:

class HomeView extends GetView<HomeController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        key: Key('drawer'),
        child: Column(
          children: [
            Image.asset(
              AppUi.assets.logo,
              height: 600.w,
              width: Get.width,
              fit: BoxFit.cover,
            ),
            DrawerListTile('الأسئلة', Icons.question_answer, () {
              Get.back();
              Get.toNamed(Routes.QUESTIONS);
            })
          ],
        ),
      ),
      backgroundColor: AppUi.colors.appSkyLight,
      appBar: TafraAppBar(
        titleText: 'الرئيسية',
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              AzkarCard(
                'أذكار الصباح',
                Icons.wb_sunny,
                onTap: () => controller.routeToAzkar(AzkarType.morning),
                key: Key('morning'),
              ),
              AzkarCard(
                'أذكار المساء',
                Icons.nightlight_round,
                onTap: () => controller.routeToAzkar(AzkarType.evening),
                key: Key('evening'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

和是自定义DrawerListTileAzkarCard部件。当我删除它们并与Text小部件交换它们时,测试代码成功了!。

所以我不知道我要测试的代码是否不应该有自定义小部件。这在实际项目中是不可能的。

4

1 回答 1

5

问题已经解决了!

这个问题的原因ScreenUtilInitMaterialApp. pumpWidget方法应该是这样的:

_pumpWidget(WidgetTester tester,) => tester.pumpWidget(
        MaterialApp(
          home: ScreenUtilInit(
              designSize: Size(1080, 1920), builder: () => HomeView()),
        ),
      );
于 2021-06-17T09:15:32.867 回答