Home > database >  Flutter - How to test Hydrated Bloc?
Flutter - How to test Hydrated Bloc?

Time:10-20

I want to mock Hydrated Blocs but I receive an error when running those unit tests.

Storage was accessed before it was initialized.
Please ensure that storage has been initialized.

For example:

final storage = await HydratedStorage.build();
HydratedBlocOverrides.runZoned(
  () => runApp(MyApp()),
  storage: storage,
);

I found a solution on GitHub, but it also displays an error https://github.com/felangel/bloc/blob/master/examples/flutter_weather/test/helpers/hydrated_bloc.dart

void initHydratedStorage() {
  TestWidgetsFlutterBinding.ensureInitialized();
  hydratedStorage = MockStorage();
  when(
    () => hydratedStorage.write(any(), any<dynamic>()),
  ).thenAnswer((_) async {});
  HydratedBloc.storage = hydratedStorage;
}

Gives this: The setter 'storage' isn't defined for the type 'HydratedBloc<Event, State>

CodePudding user response:

I upgraded my hydrated bloc package from 8.1.0 to 9.0.0-dev.3 and it seems to work now. The problem was that the storage property is not available in the 8.1.0 version.

  • Related