how to write unit tests for different bloc state in flutter

44 Views Asked by At

I have a file which shows some texts , and for state management Im using Bloc , and for my bloc I have several states such as :

1 - initialState / 2 - stateInProgress / 3 - stateDone

but my problem is if i want to find a widget from the doneState its not possible , when the screen is build the initState is executed , then with stateInProgress a lading widget shows , and after that when the texts are fetched , loading is gone and we can see the texts

I can go from initState for stateInProgress but I can go to the stateDone

BlocBuilder<FakeBloc, FakeState>(
            builder: (context, state) {
              if (state is stateInProgress) {
                return LoadingWidget();
              }

              if (state is stateFailure) {
                return ErrorWidget();
              }

              if (state is stateDone) {
                return Text('fake text')
             }
);


and this is my test code :

  testWidgets('shows done state correctly', (WidgetTester tester) async {
    await tester.pumpWidget(createTestableWidget());

    await tester.pump();

    expect(find.byType(LoadingWidget), findsOneWidget);
    await tester.pump();
    await tester.pumpAndSettle();

    expect(find.byText('fake text')), findsOneWidget);
  });

in the test , the expect for the LoadingWidget passes , but i cant fine the text

any idea?

I tried pump and pumpWithSettle with duration , didn't work

0

There are 0 best solutions below