Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Firebase进行机器人分子app测试_Android_Firebase_Firebase Realtime Database_Robolectric_Firebase Authentication - Fatal编程技术网

Android 使用Firebase进行机器人分子app测试

Android 使用Firebase进行机器人分子app测试,android,firebase,firebase-realtime-database,robolectric,firebase-authentication,Android,Firebase,Firebase Realtime Database,Robolectric,Firebase Authentication,我正在尝试为我的演示者编写一个简单的机器人测试,它使用Firebase数据库和Firebase Auth。但每次我试着开始测试时,它都会通过一个非法的例外 java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. at com.google.firebase.FirebaseApp.getInstance(Unknown Source) at com.google.fireba

我正在尝试为我的演示者编写一个简单的机器人测试,它使用Firebase数据库和Firebase Auth。但每次我试着开始测试时,它都会通过一个非法的例外

java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
    at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
    at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
我的测试很简单

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class LoginPresenterTest {
    private LoginPresenter presenter;
    private LoginMvpView view;

    @Before
    public void beforeEachTest() {
        presenter = new LoginPresenter();
        view = new LoginFragment();
    }

    @Test
    public void attachView_shouldAttachViewToThePresenter() {
        presenter.attachView(view);
        assertSame(presenter.getMvpView(), view);
    }
}
而在我的presenter构造函数中,我只得到Firebase实例

public LoginPresenter() {
        this.firebaseAuth = FirebaseAuth.getInstance();
        this.database = FirebaseDatabase.getInstance().getReference();
    }

有没有办法将Robolectric与Firebase一起使用?

如果在代码中不使用它们进行测试,则可以通过构造函数注入它们:

public LoginPresenter(FireBaseAuth firebaseAuth, FirebaseDatabase database){
    this.firebaseAuth = firebaseAuth;
    this.database = database;
}
您为它们注入
null
,记住使用
null
是一种非常糟糕的方法。 更好的方法是使用类库或使用接口/包装器等

例如,使用接口

public interface IDatabase {
    public List<String> getData();
}
IDatabase
的正常实现:

public class MyDatabase implements IDatabase {

    private FirebaseDatabase database;

    public MyDatabase(FirebaseDatabase database) {
        this.database = database;
    }

    public List<String> getDate() {
        // Use the FirebaseDatabase for returning the getData
        return ...;
    }
}
public class DatabaseMock implements IDatabase {
    public List<String> getData() {
        // Return the expected data from the mock
        return ...;
    }
}
从测试中调用它,如:

 presenter = new LoginPresenter(FirebaseAuth.getInstance(), new DatabaseMock());

不可能模拟firebase组件吗?你想在考试中用到它们吗?不,我还不需要。你能提供更多关于如何嘲笑他们的信息吗?我是测试新手。
 presenter = new LoginPresenter(FirebaseAuth.getInstance(), new DatabaseMock());