如何在小部件测试中包含提供者?
How to include a Provider(s) within widget testing?
我有一个依赖提供商的小部件。我想 运行 在此小部件上进行小部件测试。我假设因为 widget 测试直接在树中的那个节点上进行,所以出现如下错误是合理的:
* Ensure the Provider<Appointments> is an ancestor to this InstallMonitorPage Widget
(注意:我的Provider名称是.可能主要包括:
return MultiProvider(
providers: [
Provider<AuthBase>(
builder: (context) => Auth(),
),
Provider<Appointments>(
builder: (context) => Appointments(),
)
],
child: MaterialApp(
title: title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LaunchPage()),
);
当 运行对小部件树下方的小部件进行小部件测试时,是否有 syntax/way 来包含提供程序?
您可以尝试这样的操作:
await tester.pumpWidget(MultiProvider(
providers: [
Provider<AuthBase>(
builder: (context) => Auth(),
),
Provider<Appointments>(
builder: (context) => Appointments(),
)
],
child: Builder(
builder: (_) => YourWidgeToTest(),
),
),);
我有一个依赖提供商的小部件。我想 运行 在此小部件上进行小部件测试。我假设因为 widget 测试直接在树中的那个节点上进行,所以出现如下错误是合理的:
* Ensure the Provider<Appointments> is an ancestor to this InstallMonitorPage Widget
(注意:我的Provider名称是.可能主要包括:
return MultiProvider(
providers: [
Provider<AuthBase>(
builder: (context) => Auth(),
),
Provider<Appointments>(
builder: (context) => Appointments(),
)
],
child: MaterialApp(
title: title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LaunchPage()),
);
当 运行对小部件树下方的小部件进行小部件测试时,是否有 syntax/way 来包含提供程序?
您可以尝试这样的操作:
await tester.pumpWidget(MultiProvider(
providers: [
Provider<AuthBase>(
builder: (context) => Auth(),
),
Provider<Appointments>(
builder: (context) => Appointments(),
)
],
child: Builder(
builder: (_) => YourWidgeToTest(),
),
),);