Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] testing navigation issue #767

Closed
rodrigobastosv opened this issue Jan 7, 2020 · 3 comments
Closed

[Question] testing navigation issue #767

rodrigobastosv opened this issue Jan 7, 2020 · 3 comments
Assignees
Labels
pkg:bloc_test This issue is related to the bloc_test package question Further information is requested
Projects

Comments

@rodrigobastosv
Copy link
Contributor

First of all, thanks for the effort in creating and maintaining such a great package.

My question is this, suppose i have the following widget (the code is just to give an idea, i writed fast, just want to give an idea)

class SigninPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Theme.of(context).primaryColor,
        child: BlocProvider<SigninBloc>(
          create: (_) => SigninBloc(SigninRepository())),
          child: BlocListener<SigninBloc, SigninState>(
            listener: (context, state) {
              if (state is UserSignedSuccessfullyState) {
                Navigator.of(context).push(MaterialPageRoute(builder: (_) => NextPage()))
              }
            },
            child: Container(),
          ),
        ),
      ),
    );
  }
}

I want to unit test this line Navigator.of(context).push(MaterialPageRoute(builder: (_) => NextPage()))

So i made this test case:

testWidgets(
        'shows NextPage when UserSignedSuccessfullyState yielded',
        (WidgetTester tester) async {
      when(mockHomeBloc.state).thenReturn(UserSignedSuccessfullyState());
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Container(
              child: BlocProvider<SigninBloc>(
                  create: (_) => mockSigninBloc,
              child: BlocListener<SigninBloc, SigninState>(
                listener: (context, state) {
                  if (state is UserSignedSuccessfullyState) {
                    Navigator.of(context).push(MaterialPageRoute(builder: (_) => NextPage()))
                  }
                },
                child: Container(),
              ),
            ),
          ),
        ),
      );

      expect(find.byType(NextPage), findsOneWidget);
    });

What i would expect is to have the navigator line executed since i mocked the state of the bloc to the exact state i'm checking on BlocListener.

Am i messing something up? Or i just misunderstood some concepts?

Thanks!

@rodrigobastosv
Copy link
Contributor Author

After more analysis i think i know the problem. I guess BlocListener is only executed after the state changes. So i guess mocking isn't gonna work

@felangel
Copy link
Owner

felangel commented Jan 7, 2020

Hey @rodrigobastosv 👋
Thanks for opening an issue and for the positive feedback!

If you want to mock the BlocListener you should create a MockBloc instance and then use whenListen from the bloc_test package to stub the state stream.

whenListen(mockHomeBloc, Stream<HomeState>.fromIterable([InitialHomeState(), UserSignedSuccessfullyState()]);

Hope that helps 👍

@felangel felangel self-assigned this Jan 7, 2020
@felangel felangel added question Further information is requested pkg:bloc_test This issue is related to the bloc_test package labels Jan 7, 2020
@felangel felangel added this to To do in bloc via automation Jan 7, 2020
@felangel felangel moved this from To do to In progress in bloc Jan 7, 2020
@felangel felangel moved this from In progress to Done in bloc Jan 7, 2020
@rodrigobastosv
Copy link
Contributor Author

Thanks @felangel , i closes the issue because i solved my problem in a diferent way. But i'll refactor to use this. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:bloc_test This issue is related to the bloc_test package question Further information is requested
Projects
bloc
  
Done
Development

No branches or pull requests

2 participants