使用 ChangeNotifierProvider 测试小部件
尽管有所有教程,我还是不明白小部件测试如何与 ChangeNotifier Provider 一起使用。我是初学者。
这是我需要测试的小部件。
class SummaryScreen extends StatelessWidget {
const SummaryScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
List<Person> tenants = context.watch<TenantSTATE>().tenants;
return Scaffold(
body: Text(tenants.length)
);
}
这是我的测试:
void main() async {
testWidgets("Flutter Widget Test", (WidgetTester tester) async {
await tester.pumpWidget(SummaryScreen());
expect(find.widgetWithIcon(FloatingActionButton, Icons.add), findsOneWidget);
});
}
我收到此错误:
在此之上找不到正确的提供者 摘要屏幕小部件
我应该做什么?
I do not understand how widget tests works with ChangeNotifier Provider despite all tutorials. I am beginner.
Here is the widget I need to test.
class SummaryScreen extends StatelessWidget {
const SummaryScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
List<Person> tenants = context.watch<TenantSTATE>().tenants;
return Scaffold(
body: Text(tenants.length)
);
}
Here is my test :
void main() async {
testWidgets("Flutter Widget Test", (WidgetTester tester) async {
await tester.pumpWidget(SummaryScreen());
expect(find.widgetWithIcon(FloatingActionButton, Icons.add), findsOneWidget);
});
}
I am getting this error :
Could not find the correct Provider above this
SummaryScreen Widget
What should I do ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用 PumpWidget 时将所需的提供程序添加到小部件树中,就像您在应用程序中所做的那样,如下所示:
Add needed providers to the widget tree when calling pumpWidget, like you do it in your app, something like this: