【问题标题】:Flutter Widget Test passes on device but not without oneFlutter Widget Test 在设备上通过但并非没有
【发布时间】:2022-02-13 19:59:31
【问题描述】:

我有一个小部件,它显示来自 api 的数据列表,我正在尝试为它的各种状态编写测试,从它的空状态开始。

目前我的测试抽出有问题的小部件,小部件进行了我正在模拟的网络调用,然后它检查是否显示了空状态文本。

当我在使用的设备上运行测试时,此测试通过

 flutter run --flavor myTestApp -t test/booking/booking_list_widget_test.dart

但是当我从 IDE (IntelliJ) 运行它时失败,失败异常是:

The following TestFailure object was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: _TextFinder:<zero widgets with text "You're all caught up." (ignoring offstage widgets)>
Which: means none were found but one was expected

它似乎没有等待给 tester.pump 的 Duration,我尝试将它包装在 tester.runAsync 中并使用 pump 和 solve 等,但我无法让它通过。

欢迎任何想法我不能分享小部件树,但我可以分享测试

  void main() {
    setupFirebaseAuthMocks();
  
    setUpAll(
      () async {
        SharedPreferences.setMockInitialValues(
          {
            Constants.USER_ID: '1234567890',
          },
        );
      },
    );
  
    testWidgets(
      'emptyListTest',
      (tester) async {
        await _initializeDependencies(tester);
  
        final dio = di.getIt.get<Dio>();
        final dioAdapter = DioAdapter(dio: dio);
  
        dioAdapter.onGet(
          '/url/user/1234567890',
          (server) => server.reply(
            200,
            BookingResponse(
              (b) => b
                ..data = <Booking>[].toBuiltList().toBuilder()
                ..pagination = Pagination((b) => b
                  ..last = ''
                  ..first = ''
                  ..next = ''
                  ..skip = 0
                  ..count = 0).toBuilder(),
            ),
          ),
        );
  
        final testApp = await tester.runAsync(
          () => wordskiiTestApp(
            widgetUnderTest: BookingView(),
          ),
        );
  
        await tester.pumpWidget(testApp!);
        await tester.pump(const Duration(seconds: 1));
  
        expect(find.text('AVAILABLE'), findsOneWidget);
        // debugDumpApp();
  
        expect(
          find.text(
            'You\'re all caught up.',
          ),
          findsOneWidget,
        );
      },
    );
  }
  
  Future<void> _initializeDependencies(WidgetTester tester) async {
    await tester.runAsync(di.getIt.reset);
    await tester.runAsync(di.initTesting);
    await tester.runAsync(di.getIt.allReady);
  }

【问题讨论】:

  • 来自 IntelliJ,它运行什么命令来执行测试?我很确定它与您使用的设备测试命令不同。
  • 我认为 IntelliJ 正在运行“单元测试”。并且使用 Flutter 的小部件单元测试,异步代码无法正常运行。很多文章都提到了这个问题。
  • yh 所以他们运行假异步,你可以通过使用 tester.runAsync 获得正常功能,但这应该在设备上或设备上运行相同,因为这些是小部件测试而不是集成颤振驱动程序测试跨度>
  • 能否请您提供完整的最小可重复样本?
  • 请转储整个小部件树,看看发生了什么

标签: flutter async-await widget-test-flutter


【解决方案1】:

'You\'re all caught up' 预期在哪个小部件上?我之前在 ListView 项目上尝试 find.textContaining() 时遇到过类似的问题。我能够通过找出 Text 应该出现的小部件来解决我的问题。

就我而言,我必须使用find.widgetWithText(InkWell, String)。要找出它是哪个小部件,您可以在运行测试时点击屏幕上显示的小部件(即在模拟器上)。日志应显示点击的小部件。

【讨论】:

  • 当您在模拟器上点击该小部件时,它还会为您提供查找小部件的选项,不幸的是,这只是一个文本小部件,如果测试等待泵,它将在那里,就像它一样在设备上运行时
  • 赏金到期恭喜
猜你喜欢
  • 2021-05-29
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 2020-08-12
相关资源
最近更新 更多