Flutter Widget测试在设备上通过,但并非没有



我有一个小部件,它显示来自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和settle等,但我无法通过。

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

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);
}

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

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

最新更新