[英]Firebase auth unit testing error No Firebase App
I'm trying to test my firebase auth
methods.我正在尝试测试我的
firebase auth
方法。 Auth methods are signin, signout , register, etc. this are methods i want to perform unit test.身份验证方法是登录、注销、注册等。这是我想要执行单元测试的方法。
I'm getting error No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
我收到错误
No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
I tried to initialize Firebase.initializeApp
in test main
method its also doesn't work.我试图在 test
main
方法中初始化Firebase.initializeApp
它也不起作用。
class MockUserRepository extends Mock implements AuthService {
final MockFirebaseAuth auth;
MockUserRepository({this.auth});
}
class MockFirebaseAuth extends Mock implements FirebaseAuth{}
class MockFirebaseUser extends Mock implements FirebaseUser{}
class MockFirebase extends Mock implements Firebase{}
void main() {
MockFirebase firebase=MockFirebase();
MockFirebaseAuth _auth = MockFirebaseAuth();
BehaviorSubject<MockFirebaseUser> _user = BehaviorSubject<MockFirebaseUser>();
when(_auth.onAuthStateChanged).thenAnswer((_){
return _user;
});
AuthService _repo = AuthService.instance(auth: _auth);
group('user repository test', (){
when(_auth.signInWithEmailAndPassword(email: "email",password: "password")).thenAnswer((_)async{
_user.add(MockFirebaseUser());
});
when(_auth.signInWithEmailAndPassword(email: "mail",password: "pass")).thenThrow((){
return null;
});
test("sign in with email and password", () async {
var signedIn = await _repo.onLogin(email:"testuser@test.com",password: "123456");
expect(signedIn, isNotNull);
expect(_repo.status, Status.Authenticated);
});
test("sing in fails with incorrect email and password",() async {
var signedIn = await _repo.onLogin(email:"testuser@test.com",password: "666666");
expect(signedIn, false);
expect(_repo.status, Status.Unauthenticated);
});
test('sign out', ()async{
await _repo.signout();
expect(_repo.status, Status.Unauthenticated);
});
});
}
AuthService class AuthService 类
enum Status { Uninitialized, Authenticated, Authenticating,
Unauthenticated }
class AuthService with ChangeNotifier {
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseUser _user;
FirebaseUser get user => _user;
set user(FirebaseUser value) {
_user = value;
}
Status _status = Status.Uninitialized;
Future<User> getCurrentUser() async {
User currentUser;
await FirebaseAuth.instance.authStateChanges().listen((User user) {
currentUser = user;
});
return currentUser;
}
AuthService();
AuthService.instance({this.auth}) {
// auth.onAuthStateChanged.listen((user) {
// onAuthStateChanged(user);
// });
}
Future<void> signout() async {
await auth.signOut();
}
Future<User> createAccount({String email, String password}) async {
try {
UserCredential userCredential = await
auth.createUserWithEmailAndPassword(
email: email, password: password);
return userCredential != null ? userCredential.user : null;
} on FirebaseAuthException catch (e) {
showToast(e.message);
} catch (e) {
log(e.toString());
return null;
}
}
Future<User> onLogin({String email, String password}) async {
try {
User user;
await auth
.signInWithEmailAndPassword(email: email, password: password)
.then((value) {
showToast("Login sucessful");
user = value != null ? value.user : null;
});
return user;
} on FirebaseAuthException catch (e) {
showToast(e.message);
}
}
sendResetPassword({String email}) async {
bool isSent = false;
try {
await auth.sendPasswordResetEmail(email: email).then((value) {
showToast("Reset password email sent");
isSent = true;
});
return isSent;
} on FirebaseAuthException catch (e) {
showToast(e.message);
}
}
Future<void> onAuthStateChanged(FirebaseUser user) async {
if (user == null) {
_status = Status.Unauthenticated;
} else {
_user = user;
_status = Status.Authenticated;
}
notifyListeners();
}
Status get status => _status;
set status(Status value) {
_status = value;
}
}
Have a look at how they tested directly in firebase_auth code : https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_auth/firebase_auth/test看看他们如何直接在 firebase_auth 代码中进行测试: https : //github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_auth/firebase_auth/test
Call the setupFirebaseAuthMocks
(you can adapt the code from here ) method at the beginning of your main
method and call await Firebase.initializeApp();
调用
setupFirebaseAuthMocks
(你可以从适应代码在这里你的开头方法) main
方法和调用await Firebase.initializeApp();
in a setUpAll
method.在
setUpAll
方法中。
Firebase.initializeApp()
is the solution for this. Firebase.initializeApp()
是解决方案。 I am pretty sure.我很确定。 Try calling it in the file your are doing all these functions in their initState().
尝试在您在 initState() 中执行所有这些功能的文件中调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.