I have been trying to set up a unit test for my Firebase User authentication class. I have tried to do it like many tutorials have shown, but since there hase been an update to the firebase core plugin they have changed the way you must set up the environment. I have not been able to figure out how to call the Firebase.initializeApp() method using Mocks. Documentation provided by the plugin also doesnt reveal any solutions. Has anyone solved this one so far ?
class MockFirebaseAuth extends Mock implements fireAuth.FirebaseAuth {}
class MockFirebaseUser extends Mock implements fireAuth.User {}
class MockAuthResult extends Mock implements fireAuth.UserCredential {}
void main() async {
TestWidgetsFlutterBinding.ensureInitialized();
MockFirebaseAuth _auth = MockFirebaseAuth();
BehaviorSubject<MockFirebaseUser> _user = BehaviorSubject<MockFirebaseUser>();
when(_auth.authStateChanges()).thenAnswer((_) {
return _user;
});
User user = User.instance(auth: _auth);
group('User Model Test', () {
String email = 'test@email.com';
String password = '123456';
when(_auth.signInWithEmailAndPassword(email: email, password: password)).thenAnswer((_) async {
_user.add(MockFirebaseUser());
return MockAuthResult();
});
test('sign in with email and password', () async {
bool signedIn = await user.signIn(email, password);
expect(signedIn, true);
expect(user.status, Status.Authenticated);
});
test('sign out', () async {});
});
}
Throwed Error:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()