我有以下代码:
class SimpleTest {
@Test
fun observable_isPass() {
val store = FirebaseFirestore.getInstance()
assert(true)
}
}
当我尝试运行测试时,它在下面抛出异常:
Method getMainLooper in android.os.Looper not mocked.
如何在单元测试中测试 firestore 数据?
最佳答案
可能是一个迟到的答案,但 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()
关于unit-testing - 如何使用 JUnit 在 android 中对 Firestore 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55586851/