Firebase 身份验证单元测试错误 No Firebase App
Posted
技术标签:
【中文标题】Firebase 身份验证单元测试错误 No Firebase App【英文标题】:Firebase auth unit testing error No Firebase App 【发布时间】:2020-12-17 10:28:34 【问题描述】:我正在尝试测试我的 firebase auth
方法。身份验证方法是登录、注销、注册等。
这是我要执行单元测试的方法。
我收到错误No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
我尝试在测试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 类
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;
【问题讨论】:
哪一行代码导致了这个错误? @DougStevensonAuthService _repo = AuthService.instance(auth: _auth);
这行请帮忙。我还添加了我的AuthService
类。
【参考方案1】:
看看他们如何直接在 firebase_auth 代码中进行测试:https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_auth/firebase_auth/test
在main
方法的开头调用setupFirebaseAuthMocks
(您可以从here 改编代码)方法并在setUpAll
方法中调用await Firebase.initializeApp();
。
【讨论】:
好东西,谢谢。为了澄清起见,您可以在那里选择mock_auth.dart
文件并将其带到您的测试套件并使用它。【参考方案2】:
Firebase.initializeApp()
是解决此问题的方法。我很确定。尝试在文件中调用它,您正在它们的 initState() 中执行所有这些功能。
【讨论】:
我已经尝试在我的测试文件中Firebase.initializeApp()
它也没有工作。以上是关于Firebase 身份验证单元测试错误 No Firebase App的主要内容,如果未能解决你的问题,请参考以下文章
如何对大多数视图使用 Firebase 身份验证的 Swift 应用程序进行单元测试?
Firebase + Flutter - 云函数 onCall 导致 Android 应用出现“未经身份验证”错误
使用 SMS Firebase 进行身份验证:错误 (auth/argument-error)