New*_*Dev 11 flutter flutter-test
所以,我正在尝试测试我的颤振应用程序。这是我所做的
class MockSplashScreenBloc extends MockBloc<SplashScreenState>
implements SplashScreenBloc {}
void main() {
MockSplashScreenBloc splashScreenBloc;
Widget MyWidget() {
return MaterialApp(
home: BlocProvider(
create: (context) {
return SplashScreenBloc(url: "google.com");
},
child: SplashScreen(),
),
);
}
group('Splash Screen Widget Test', () {
setUp(() {
splashScreenBloc = MockSplashScreenBloc();
});
tearDown(() {
splashScreenBloc?.close();
});
testWidgets('should render Container when state is Default State',
(WidgetTester tester) async {
when(splashScreenBloc.state).thenAnswer((_) => Default());
await tester.pumpWidget(MyWidget());
expect(find.byKey(ValueKey("container_empty")), findsOneWidget);
});
testWidgets('should render LoadingIndicator when state is Loading State',
(WidgetTester tester) async {
when(splashScreenBloc.state).thenReturn(LoadingState());
await tester.pumpWidget(MyWidget());
expect(find.byKey(ValueKey("splash_loading_bar")), findsOneWidget);
});
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的 SplashScreen
class SplashScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: BlocBuilder<SplashScreenBloc, SplashScreenState>(
builder: (context, state) {
if (state is LoadingState) {
return CircularProgressIndicator(
key: Key("splash_loading_bar"),
);
} else if (state is NotConnected) {
return Text("Could not connect to server",
key: ValueKey("splash_screen_not_connected"));
} else if (state is Connected) {
return Text(
"Connected",
key: Key("splash_screen_connected"),
);
} else {
return Container(key: Key("container_empty"));
}
},
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法通过此测试should render LoadingIndicator when state is Loading State,我已经尝试使用expect(find.byType(CircularProgressIndicator), findsOneWidget);但它仍然无法正常工作,这是错误
???异常被颤振测试框架捕获 ???????????????????????????????????????????????? ????????? 运行测试时抛出以下 TestFailure 对象:预期:小部件树中正好有一个匹配节点实际:_KeyFinder:<零小部件,键为 [<'splash_loading_bar'>](忽略舞台外小部件)>
其中:意味着没有找到,只有一个预料之中
我该如何解决?
您尝试过await tester.pumpAndSettle()吗?
tester.pump()
在给定的持续时间后触发小部件的重建。tester.pumpAndSettle()
在给定的持续时间内重复调用 Pump,直到不再有任何帧被调度。这本质上是等待所有动画完成。
请尝试以下代码:
testWidgets('should render LoadingIndicator when state is Loading State',
(WidgetTester tester) async {
when(splashScreenBloc.state).thenReturn(LoadingState());
await tester.pumpWidget(MyWidget());
await tester.pumpAndSettle();
expect(find.byKey(ValueKey("splash_loading_bar")), findsOneWidget);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
749 次 |
| 最近记录: |