【发布时间】: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