jeudi 23 janvier 2020

How to make widget test wait until Bloc has updated the state?

I have a EmailScreen (stateful widget) that has a text input, and a button. The button is only enabled when a valid email is input.

I'm using Bloc, and my screen has InitialEmailState and ValidEmailInputState, and it works fine when I run the app.

In my widget test, the second expectation is failing before bloc has a chance to update the state:


  testWidgets('when valid email is input, button is enabled', (tester) async {
    const validEmail = 'email@provider.com';

    emailBloc.listen((event) {
      print('NEW EVENT: ' + event.toString());
    });

    await bootUpWidget(tester, emailScreen);

    final BottomButton button = tester.widget(
        find.widgetWithText(BottomButton, 'CONTINUE'));

    expect(button.enabled, isFalse);

    await tester.enterText(find.byType(TextInputScreen), validEmail);
    await tester.pumpAndSettle();

    expect(button.enabled, isTrue);
  });

And here's the output I'm getting:

NEW EVENT: InitialEmailState
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: true
  Actual: <false>

...

The test description was:
  when valid email is input, button is enabled
════════════════════════════════════════════════════════════════════════════════════════════════════
Test failed. See exception logs above.
The test description was: when valid email is input, button is enabled

NEW EVENT: InputValidEmailState
✖ when valid email is input, button is enabled
Exited (1)

As you can see, it prints the initial state, fails the second expectation, and then prints the expected state.

Thanks in advance :)

Aucun commentaire:

Enregistrer un commentaire