找不到小部件按键颤动测试

时间:2020-11-10 03:26:45

标签: flutter flutter-test

因此,我正在尝试测试我的flutter应用程序。这是我的工作

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()
gauth.CommandLineAuth()

drive = GoogleDrive(gauth)

file_list = drive.ListFile({'q': "'XXXXXXXXXXXXXXXXXXXXXXXX' in parents and trashed=false"}).GetList()
for file2 in file_list:
    string = "'"+ file2['id'] +"'" + " in parents and trashed=false"
    file_list2 = drive.ListFile({'q': string}).GetList()
    print(file2['title'], file2['id'])
    for file3 in file_list2:
        file3.GetContentFile('/data/' + file3['title'])
        print('downloaded: ', file3['title'], file3['id'])

这是我的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); }); }); }

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")); } }, ), ), ); } } ,我很努力尝试使用should render LoadingIndicator when state is Loading State,但仍然无法正常工作,这是错误消息

══╡颤振测试框架引起的异常 ╞═════════════════════════════════════════════════ following以下 抛出TestFailure对象运行测试:预期:正好一个 小部件树中的匹配节点实际:_KeyFinder:<零小部件 使用键[<'splash_loading_bar'>](忽略后台小部件)>
其中:表示未找到任何东西,但预期会有一个

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您尝试等待tester.pumpAndSettle()吗?

tester.pump()
在给定的持续时间后触发窗口小部件的重建。

tester.pumpAndSettle()
重复调用给定持续时间的泵,直到没有 更长的预定帧。这基本上等待所有动画 完成。

请尝试以下代码:

   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);
    });