Tob*_*obi 5 testing dart flutter
我在我的 flutter 项目中使用auto_route包并将我的应用程序包装在MaterialApp.router.
现在我想测试一个AutoRouter.of(context).pop();在一个地方调用的小部件。
我的测试如下所示:
testWidgets('my widget test',
(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: MyWidget(),
),
);
});
Run Code Online (Sandbox Code Playgroud)
可以理解的是,泵送小部件会抛出以下错误消息:
AutoRouter operation requested with a context that does not include an AutoRouter. The context used to retrieve the Router must be that of a widget that is a descendant of an AutoRouter widget.
所以我需要AutoRouter在测试中在我的小部件上方提供一个实例。
我的小部件也使用了MediaQuery,我需要将测试的小部件包装在里面MaterialApp,以避免由于MediaQuery.of(context)查找而引发的错误。
我想我需要做一些类似的事情AutoRouter,但还没有弄清楚。有类似的东西吗MockAutoRouter?
很高兴获得任何帮助。
好吧,我成功了。
AutoRouter.of(context)查找StackRouter通过 提供的实例StackRouterScope。所以我嘲笑StackRouter并通过以下方式提供StackRouterScope:
@GenerateMocks([StackRouter])
...
await tester.pumpWidget(
StackRouterScope(
controller: MockStackRouter(),
stateHash: 0,
child: MyWidget(),
),
);
Run Code Online (Sandbox Code Playgroud)
现在AutoRouter.of(context).pop()不再扔了。
现在应该还可以存根并验证是否调用了MockStackRoutereg 。pop
| 归档时间: |
|
| 查看次数: |
1515 次 |
| 最近记录: |