I’m encountering an error when running a Flutter test, but the app works fine when running normally. The error message I’m seeing is:
package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart 210:7 FirebaseCoreHostApi.initializeCore PlatformException(channel-error, Unable to establish connection on channel., null, null)
I’ve included the test code that’s causing the issue above. Essentially, it’s a unit test for a method called verifyStore in a Flutter application. The method interacts with Firebase services, and the test uses mock objects to simulate these interactions.
Does anyone have any insights into why this error might be occurring in the test environment but not in the app itself?
Here’s the test code that’s causing the issue:
import 'administration_remote_data_source_repository_impl_test.mocks.dart';
@GenerateMocks([AdministrationRemoteDataSourceRepositoryImpl])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
});
late AdministrationRemoteDataSourceRepositoryImpl repositoryImpl;
late MockAdministrationRemoteDataSourceRepositoryImpl mockRemoteService;
setUp(() {
mockRemoteService = MockAdministrationRemoteDataSourceRepositoryImpl();
repositoryImpl = AdministrationRemoteDataSourceRepositoryImpl(
remoteService: mockRemoteService,
firebaseMessaging: FirebaseMessaging.instance,
firebaseAuth: FirebaseAuth.instance,
firebaseFirestore: FirebaseFirestore.instance,
firebaseStorage: FirebaseStorage.instance,
);
});
group('verifyStore', () {
test('should return Success(true) when verification is successful', () async {
// Arrange
when(mockRemoteService.verifyStore(any, any))
.thenAnswer((_) async => Right(Success(message: "Done")));
// Act
final result = await repositoryImpl.verifyStore('Ro1Q3yEedvYF1OBtyB2fRJogacP2', true);
// Assert
expect(result, isA<Right<Failure, Success>>());
expect((result as Right).value.message, "Done");
});
test('should return Failure when verification fails', () async {
// Arrange
when(mockRemoteService.verifyStore(any, any))
.thenAnswer((_) async => Left(Failure(message: "Failed")));
// Act
final result = await repositoryImpl.verifyStore('Ro1Q3yEedvYF1OBtyB2fRJogacP2', true);
// Assert
expect(result, isA<Left<Failure, Success>>());
expect((result as Left).value.message, "Failed");
});
});
}