Firebase身份验证单元测试错误无Firebase应用 [英] Firebase auth unit testing error No Firebase App

查看:60
本文介绍了Firebase身份验证单元测试错误无Firebase应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试我的 firebase auth 方法.身份验证方法包括登录,注销,注册等.这是我要执行单元测试的方法.

I'm trying to test my firebase auth methods. Auth methods are signin, signout , register, etc. this are methods i want to perform unit test.

我收到错误消息未创建Firebase应用程序'[DEFAULT]'-调用Firebase.initializeApp()

我尝试在测试 main 方法中初始化 Firebase.initializeApp ,它也无法正常工作.

I tried to initialize Firebase.initializeApp in test main method its also doesn't work.

 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类

AuthService class

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

推荐答案

看看它们如何直接在firebase_auth代码中进行测试:

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

调用 setupFirebaseAuthMocks (您可以从

Call the setupFirebaseAuthMocks (you can adapt the code from here) method at the beginning of your main method and call await Firebase.initializeApp(); in a setUpAll method.

这篇关于Firebase身份验证单元测试错误无Firebase应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆