Bloc: Strange behavior testing bloc

Created on 10 Jun 2020  ยท  2Comments  ยท  Source: felangel/bloc

Hi Felix, thanks for your great package and great work.

I have a question regarding testing blocs. Let me show my case:

I have this test:

    testWidgets('should show snackbar when success', (tester) async {
      final mockEditProfileBloc = MockEditProfileBloc();
      whenListen(
        mockEditProfileBloc,
        Stream.fromIterable(
          [
            UpdatingUser(),
            UserUpdateSuccess(),
          ],
        ),
      );
      await tester.pumpWidget(
        connectedWidget(
          PageConnected<EditProfileBloc>(
            bloc: mockEditProfileBloc,
            page: EditProfilePage(
              imagePicker: MockImagePicker(),
            ),
          ),
        ),
      );
      await tester.pumpAndSettle();
      final snackSuccess = find.text(USER_UPDATED_WITH_SUCCESS);
      expect(snackSuccess, findsOneWidget);
    });

And want to test this page:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: DefaultBottombar(USER_PROFILE_PAGE),
      body: SafeArea(
        child: BlocConsumer<EditProfileBloc, EditProfileState>(
          listener: (consumerContext, state) {
            print('LISTENER');
            print(state);
            state.continued(
              (_) => {},
              (_) =>
                  Snackbar.success(consumerContext, USER_UPDATED_WITH_SUCCESS),
              (_) => {},
              (_) => Snackbar.error(consumerContext, USER_UPDATE_FAILED),
            );
          },
          builder: (_, state) {
            print('BUILDER');
            print(state);
            return state.join(
              _mapStateToWidget,
              _mapStateToWidget,
              _mapUpdatingUserToWidget,
              _mapUserUpdateFailedToWidget,
            );
          },
        ),
      ),
    );
  }

My test is failing because at some point the bloc is yielding a null state as you can see on the console log of the test execution:

BUILDER
null
LISTENER
UserUpdateSuccess
BUILDER
UserUpdateSuccess
โ•โ•โ•ก EXCEPTION CAUGHT BY WIDGETS LIBRARY โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
The following assertion was thrown building BlocBuilder<EditProfileBloc, EditProfileState>(dirty,
state: _BlocBuilderBaseState<EditProfileBloc, EditProfileState>#86c9b):
A build function returned null.
The offending widget is:
  BlocBuilder<EditProfileBloc, EditProfileState>
Build functions must never return null.
To return an empty space that causes the building widget to fill available room, return
"Container()". To return an empty space that takes as little room as possible, return
"Container(width: 0.0, height: 0.0)".

I don't know if i'm using Stream.fromIterable correctly to stub the states.

One thing i noted is that if i do:

when(mockEditProfileBloc.state).thenReturn(UserUpdateSuccess())

the listener never receive this state.

So my question is, is there something wrong with the bloc_test or am i messing thing up?

Thanks!

bloc_test question

Most helpful comment

Oh, it worked like a charm.

Thanks a lot Felix!

All 2 comments

Hi @rodrigobastosv ๐Ÿ‘‹
Thanks for opening an issue!

I believe the problem is you're not stubbing the bloc's state in addition the the stream. Can you try adding

when(mockEditProfileBloc.state).thenReturn(UserUpdateSuccess());

before the whenListen?

Oh, it worked like a charm.

Thanks a lot Felix!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  ยท  3Comments

wheel1992 picture wheel1992  ยท  3Comments

tigranhov picture tigranhov  ยท  3Comments

nerder picture nerder  ยท  3Comments

RobPFarley picture RobPFarley  ยท  3Comments