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
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:
Connectivity() within Bloc or Future to be exact, pass it as an argument to the Bloc.dev_dependency.~ (skip that as it comes together with bloc_test)Connectivity()class MockConnectivity extends Mock implements Connectivity {}
checkConnectivity() should answer with. 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
Most helpful comment
yeah, i fix it. Thanks to you