简体   繁体   English

如何从 firebase_auth 测试“signInWithEmailAndPassword”

[英]How to test "signInWithEmailAndPassword" from firebase_auth

I'm trying to write some unit test in my Flutter app.我正在尝试在我的 Flutter 应用程序中编写一些单元测试。 I'm using firebase and i write this function (that i want to test) :我正在使用 firebase 并且我编写了这个函数(我想测试):

import 'dart:core';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';

class FirebaseUtils {
  static final FirebaseAuth _auth = FirebaseAuth.instance;
  static Exception _exception;

  static Future<bool> signIn(String email, String password) async {
    FirebaseUser user;
    try{
      user = await _auth.signInWithEmailAndPassword(email: email, password: password);
    } catch (ex){
      print(ex);
      _exception = ex;
      user = null;
    }
    return user!=null;
  }
}

my test :我的测试:

import "package:test/test.dart";
import 'package:toolpad/utils/firebase/firebase_utils.dart';
import 'dart:async';

void main() {
 test("Sign In user from firebase (working)", () async {
    bool result = await FirebaseUtils.signIn("test@gmail.com", "lollollol");
    expect(result, equals(true));
  });
}

When i launch the test that throw an exception :当我启动抛出异常的测试时:

MissingPluginException(No implementation found for method signInWithEmailAndPassword on channel plugins.flutter.io/firebase_auth)
ERROR: Expected: <true>
  Actual: <false>

I have no idea how fix it, anyone have an idea ?我不知道如何解决它,有人有想法吗?

After lot of search it seems that it is not possible without specify an Firebase.initialize before.经过大量搜索后,如果不指定 Firebase.initialize 之前似乎是不可能的。

So if u want test some functions linked to Firebase, u should use testWidget .所以如果你想测试一些链接到 Firebase 的功能,你应该使用testWidget That's going to test your app not juste function)这将测试您的应用程序而不是 juste 功能)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM