I am working on the Cubit Bloc architecture on Flutter.
I have a static List with entries of multiple Cubits which I need to use throughout the application.
static List<BlocProvider> providers = [
BlocProvider<NameCubit1>(create: (_) => NameCubit1()),
BlocProvider<NameCubit2>(create: (_) => NameCubit2()),
];
Now, I need to implement a Unit test where I can test whether that static list contains a specific Cubit. I have implemented following code:
I pass this List in MultiBlocProvider -> provider field in MaterialApp.
But, everytime I run the test, it fails and tell that output is false, but it should be true. Any idea how I can implement a test where I can check whether Cubits have been added to the list.
I have tried following code for testing:
group('testing bloc provider', () {
test('check if list contains instance of LocaleCubit', () {
expect(
BlocProviders.providers.contains(BlocProvider<LocaleCubit>(create: (_) => LocaleCubit())),
true,
);
});
});
group('testing bloc provider', () {
test('check if list contains instance of LocaleCubit', () {
expect(
BlocProviders.providers.contains(BlocProvider<LocaleCubit>),
true,
);
});
});
Thanks in advance.
Calling BlocProvider()
will create a new instance which is cannot be equated with the initial providers list.
You can check for the provider type instead. The following code will return true if the provider list contains any provider that is instance of BlocProvider<LocaleCubit>
.
group('testing bloc provider', () {
test('check if list contains instance of LocaleCubit', () {
expect(
BlocProviders.providers.any((element) => element is BlocProvider<LocaleCubit>),
true,
);
});
});