找不到小部件键抖动测试

Can't found widget key flutter test

所以,我正在尝试测试我的 flutter 应用程序。这就是我所做的

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 ,我已经尝试使用 expect(find.byType(CircularProgressIndicator), findsOneWidget); 但它仍然无法正常工作,这是错误

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure object was thrown running a test: Expected: exactly one matching node in the widget tree Actual: _KeyFinder:<zero widgets with key [<'splash_loading_bar'>] (ignoring offstage widgets)>
Which: means none were found but one was expected

我该如何解决?

你试过 await tester.pumpAndSettle() 了吗?

tester.pump()
Triggers a rebuild of the widget after a given duration.

tester.pumpAndSettle()
Repeatedly calls pump with the given duration until there are no longer any frames scheduled. This essentially waits for all animations to complete.

请尝试以下代码:

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