gpt4 book ai didi

firebase - 使用 Mockito 为 Firebase 用户身份验证设置单元测试

转载 作者:行者123 更新时间:2023-12-05 06:59:04 25 4
gpt4 key购买 nike

我一直在尝试为我的 Firebase 用户身份验证类设置单元测试。我试过像许多教程所示那样做,但是由于 firebase 核心插件已经更新,他们已经改变了您必须设置环境的方式。我一直无法弄清楚如何使用 Mocks 调用 Firebase.initializeApp() 方法。插件提供的文档也没有透露任何解决方案。到目前为止,有人解决了这个问题吗?

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 {});
});
}

抛出的错误:

[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

最佳答案

你可以这样做:

 setUpAll(() async {

await Firebase.initializeApp();

});

关于firebase - 使用 Mockito 为 Firebase 用户身份验证设置单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64476408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com