3

我有一个小部件,它显示来自 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);
  }
4

1 回答 1

0

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

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

于 2022-02-11T06:41:57.250 回答