Bloc: problem with testing my bloc

Created on 8 Nov 2020  路  3Comments  路  Source: felangel/bloc

So i'm doing some test here is my test script


  Widget MyWidget() {
    return MaterialApp(
      home: BlocProvider(
        create: (context) {
          return SplashScreenBloc(url: "google.com");
        },
        child: SplashScreen(),
      ),
    );
  }
    testWidgets('should render LoadingIndicator when state is Loading State',
        (WidgetTester tester) async {
      final expectedStates = [Default(), LoadingState()];

      whenListen<SplashScreenState>(splashScreenBloc,
          Stream<SplashScreenState>.fromIterable(expectedStates));

      await tester.pumpWidget(MyWidget());

      expect(find.byKey(Key("splash_loading_bar")), findsOneWidget);
    });

I get this message

The test description was: should render LoadingIndicator when state is Loading State

my splash_screen

BlocBuilder<SplashScreenBloc, SplashScreenState>(
          builder: (context, state) {
            if (state is Default || state is LoadingState) {
              return CircularProgressIndicator(
                key: Key("splash_loading_bar"),
              );
            } else if (state is NotConnected) {
              return Text("Could not connect to server", key: Key("splash_screen_not_connected"));
            } else if (state is Connected) {
              return Text(
                "Connected",
                key: Key("splash_screen_connected"),
              );
            } else {
              return Container();
            }
          },
        )
question

Most helpful comment

going to close this. I'm separating the widget test and bloc test

All 3 comments

Hi @bobykurniawan11 馃憢
Thanks for opening an issue!

Can you please provide a link to a sample app which reproduces the issue? I鈥檓 guessing the issue is you鈥檙e using whenListen to text the BlocBuilder when all you need to do is use when instead

when(splashScreenBloc.state).thenReturn(LoadingState());

whenListen is only needed if you鈥檙e testing BlocListener code (because BlocListener requires a state change to occur in order to be triggered) whereas BlocBuilder always needs to render a widget so you can just use the regular when API from mockito.

Hope that helps 馃憤

hi @felangel , thanks for answering.

Here is my script https://github.com/bobykurniawan11/hello_chat

i already try

when(splashScreenBloc.state).thenReturn(LoadingState());

but the result still same

going to close this. I'm separating the widget test and bloc test

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ricktotec picture ricktotec  路  3Comments

Reidond picture Reidond  路  3Comments

timtraversy picture timtraversy  路  3Comments

abinvp picture abinvp  路  3Comments

RobPFarley picture RobPFarley  路  3Comments