I'm currently having an issue when I try to do unit test on a method that use Firestore. This is the method that I want to mock
Future<MediasState> loadMedias(AbstractEvent event) async {
late AbstractBlocState streamState;
try {
DataHelper _dataHelperMediasEvent = DataHelperFactory.instance
.createInstanceFromAnotherDataHelperAndEntityInstance(
_dataHelperEvents, event, 'medias');
List<AbstractMedia> medias =
(await _dataHelperMediasEvent.getAll()).cast<AbstractMedia>();
for (AbstractMedia media in medias) {
media.user = await (_dataHelperUsers.getEntity(media.user.get()));
}
medias = sortEntitiesByCreatedDateDesc(medias) as List<AbstractMedia>;
streamState = MediasShowed(medias);
} catch (error) {
streamState = MediasShowedError();
} finally {
return streamState as MediasState;
}
}
And this the test function that I wrote
test('Test load medias', () async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
AbstractEvent event =
EntityFactory.createInstance('Event') as AbstractEvent;
AbstractMedia media1 =
EntityFactory.createInstance('Media') as AbstractMedia;
AbstractMedia media2 =
EntityFactory.createInstance('Media') as AbstractMedia;
AbstractMedia media3 =
EntityFactory.createInstance('Media') as AbstractMedia;
List<AbstractMedia> listOfMedias = <AbstractMedia>[];
listOfMedias.add(media1);
listOfMedias.add(media2);
listOfMedias.add(media3);
when(mockMediasEvent.loadMedias(event))
.thenAnswer((_) async => MediasShowed(listOfMedias));
blocMedias.add(LoadMediaOfOneEvent(event));
await expectLater(blocMedias.state, isA<MediasShowedError>());
});
Unfortunately I got this error when I run this test : "PlatformException(channel-error, Unable to establish connection on channel., null, null) package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart 199:7 FirebaseCoreHostApi.initializeCore"
The DataHelperFactory create an instance of DataHelper which need an firestore instance.
class DataHelper {
DataHelper.initialize(String collectionPath) {
this.setCollection(collectionPath);
}
late String collectionPath;
late String className;
late CollectionReference<Map<String, dynamic>> collection;
final FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
I already tried to upgrade my firebase dependencies but the problem is still there. I also tried on Windows and Mac device and on a friend device and I was able to reproduce the issue. Any idea of how to fix my test will be welcome.
Thanks.