Ben*_*Ben 6 testing unit-testing mocking firebase flutter
I need to unit test an application that uses firebase. My unit tests always says "Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()". I absolutely understand why this problem occurs. But I still want to unit test my methods without really starting firebase. Even Mocking the firebase stuff did not help. Hopefully you can help me out? What am I misunderstanding?
For Example I rewrote the code from this tutorial: https://www.youtube.com/watch?v=4d6hEaUVvuU
This is my test class: ("Auth" Class in YT example is "Authentification" in my code)
import 'package:firebase_auth/firebase_auth.dart';
import 'package:hrw_textscanner/firebase/Authentification.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
class MockUser extends Mock implements User {}
final MockUser _mockUser = MockUser();
class MockFirebaseAuth extends Mock implements FirebaseAuth {
@override
Stream<User> authStateChanges() {
return Stream.fromIterable([
_mockUser,
]);
}
}
void main() {
final MockFirebaseAuth mockFirebaseAuth = new MockFirebaseAuth();
final Authentification authentification =
new Authentification(mockFirebaseAuth);
test("emit occurs", () async {
expectLater(authentification.authStateChanges, emitsInOrder([_mockUser])); //authStateChanges is the same as "user" in example
});
}
Run Code Online (Sandbox Code Playgroud)
In the Example in Youtube it works - my code still leads into that error mentioned above :(