【问题标题】:How to unit test Firestore in android using JUnit?如何使用 JUnit 在 android 中对 Firestore 进行单元测试?
【发布时间】:2019-08-30 09:29:08
【问题描述】:

我的代码如下:

class SimpleTest {
    @Test
    fun observable_isPass() {
        val store = FirebaseFirestore.getInstance()
        assert(true)
    }
}

当我尝试运行测试时,它会在下面抛出异常:

Method getMainLooper in android.os.Looper not mocked.

如何在单元测试中测试firestore数据?

【问题讨论】:

    标签: unit-testing junit google-cloud-firestore


    【解决方案1】:

    可能是一个迟到的答案,但 Firestore 使用不在主线程上的回调。为了进行单元测试,诸如 Retrofit 之类的 API 提供了在主线程上执行调用的方法,但据我所知 Firestore 并没有提供这种灵活性。这是暂停主线程直到失败或成功回调从 Firestore 返回响应的解决方法。

    val db = Firebase.firestore
    
    val latch = CountDownLatch(1)
    
    db.collection("test1").document("data")
        .set(
        // Data
        ).addOnFailureListener {
                // assertions here
                latch.countDown()
        }.addOnSuccessListener {
                // assertions here
                latch.countDown()
        }
    // Will wait for the latch to become 0
    latch.await()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多