Bloc Test Tutorial – Easier Way to Test Blocs in Dart & Flutter

Reso Coder · 28K views · 2019 · 25:04

Open on YouTube

Testing, debugging & developer toolsSoftware architecture & designPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering Flutter questions about testing Blocs with bloc_test and mocking repositories or Blocs.

Recommended by 2 people across 2 different months, 2020 to 2023.

What it is

A 25-minute Reso Coder tutorial on the bloc_test package for Dart and Flutter, showing how to test Blocs with far less boilerplate than classic Stream-based tests. It walks through a starter weather app project and uses mocks to verify emitted states.

What people said

Recommends Moti Bartov · 2020-06 · Stack Overflow · on “Flutter: how to mock Bloc”

To mock the bloc, you can use the bloc_test package [LINK] Also, you may watch this tutorial [this video] which covers bloc testing include mock bloc very nice.

Recommends FabioCSM · 2023-07 · Stack Overflow · on “Flutter test using bloc_test and mockito”

The problem here is because in the blocTest build function you didn't create the stub for the mock, instead you used the getIssues instead of the mocked version. Below is the fixed code. In case someone else is facing the same issue. For more information about bloc_test and mocks, I suggest this tutorial on youtube: Bloc Test Tutorial [this video] 'bloc: emits [IssuesInitial(), IssuesLoadedState(),] when successful', build: () { when(mockGetIssues(PageParams())) .thenAnswer((value) async => right([IssuesModel(id: 1)])); return IssuesBloc(mockGetIssues); }, act: (bloc) => bloc.add(LoadIssues()), expect: [ IssuesInitial(), isA<IssuesLoadedState>(), ], );

Cites Icare · 2020-05 · Stack Overflow · on “Why one of my state is not triggered during my test”

I followed Reso code youtube to create a small weather app, but I used the latest bloc library and flutter_bloc 4.0 also. Base App: [LINK] BLoc Test: [this video] The first 2 tests are working. For example, the below one does not give me any error: test( 'NEWER WAY BUT lONG-WINDED emits [WeatherLoading, WeatherLoaded] when successful', () { when(mockWeatherRepository.fetchWeather(any)) .thenAnswer((_) async => weather); final bloc = WeatherBloc(mockWeatherRepository); bloc.add(GetWeather('London')); emitsExactly(bloc, [ WeatherInitial(), WeatherLoading(), WeatherLoaded(weather), ]); }); For some reason, that test below does not trigger the WeatherInitial. blocTest( 'emits [WeatherLoading, Weather