bloc_test not wait for Future function

Created on 14 Nov 2020  路  2Comments  路  Source: felangel/bloc

HI @felangel it's me again, sorry for spamming your issue section . Currently I'm learning about testing.

Expected: [
SplashScreenLoading:SplashScreenLoading,
SplashScreenFinishSuccess:SplashScreenFinishSuccess
]
Actual: [SplashScreenLoading:SplashScreenLoading]
Which: at location [1] is [SplashScreenLoading:SplashScreenLoading] which shorter than expected

My bloc

class SplashScreenBloc extends Bloc<SplashScreenEvent, SplashScreenState> {
  SplashScreenBloc() : super(SplashScreenInitial());

  @override
  Stream<SplashScreenState> mapEventToState(
    SplashScreenEvent event,
  ) async* {
    if (event is CheckInternet) {
      yield SplashScreenLoading();
      final is_connected = await check();
      if (is_connected) {
        yield SplashScreenFinishSuccess();
      } else {
        yield SplashScreenFinishFailed();
      }
    }
  }

  Future<bool> check() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile) {
      return true;
    } else if (connectivityResult == ConnectivityResult.wifi) {
      return true;
    }
    return false;
  }
}

then here is my test script

```
group('Bloc Test', () {
blocTest('No Event added', build: () => SplashScreenBloc(), expect: []);
blocTest(
'add CheckInternet . Should Connected',
build: () => SplashScreenBloc(),
wait: const Duration(seconds: 5),
act: (bloc) => bloc.add(CheckInternet()),
expect: [SplashScreenLoading(), SplashScreenFinishSuccess()],
);
});


i already tryong to add wait for 5 seconds , but still no luck .

If i change my bloc to this

if (event is CheckInternet) {
  yield SplashScreenLoading();
  final is_connected = await check();
   yield SplashScreenFinishSuccess();
}

}
```

the test is passed

question

Most helpful comment

yeah, i fix it. Thanks to you

All 2 comments

Hey, it seems like Connectivity().checkConnectivity() is taking too long to execute.. And it's probably because it's not supposed to be used within test.

The proper way to fix that:

  1. Instead of creating Connectivity() within Bloc or Future to be exact, pass it as an argument to the Bloc.
  2. ~Install mockito as dev_dependency.~ (skip that as it comes together with bloc_test)
  3. Create a mocked version of Connectivity()
class MockConnectivity extends Mock implements Connectivity {}
  1. Specify what checkConnectivity() should answer with.
  2. Pass mocked Connectivity to the Bloc.

You should always use mocked methods to unit test your blocs or cubits, unless you're running integration tests.
But for unit testing you want to test the way Bloc is behaving, while not worrying of it's dependencies.

If you need more info about unit testing and how to do that correctly with Bloc I can recommend Reso Coder, here's a link for a tutorial that might help you: https://resocoder.com/2019/11/29/bloc-test-tutorial-easier-way-to-test-blocs-in-dart-flutter/

yeah, i fix it. Thanks to you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abinvp picture abinvp  路  3Comments

Reidond picture Reidond  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

zjjt picture zjjt  路  3Comments

krusek picture krusek  路  3Comments