测试 Firebase 使用协程创建用户函数并调用 await() 导致 "This job has not completed yet"

Testing usage of Firebase create user functions with Coroutines and calling await() leads to "This job has not completed yet"

我有一个简化的测试用例,我从 FirebaseAuth 调用 createUserWithEmailAndPassword(...).await() 导致异常

java.lang.IllegalStateException: This job has not completed yet

我正在使用 InstantTaskExecutorRule 但这没有帮助。

这是我的简化测试class:

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Rule
import org.junit.Test

class FirebaseCoroutineTest {
    @get:Rule
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    val auth = Firebase.auth.apply {
        this.useEmulator("10.0.2.2", 9099)
    }


    @Test
    fun testCreateUser() = runBlockingTest {
        auth.createUserWithEmailAndPassword("test@user.null", "testpwd").await()

    }
}

做这个测试的正确方法是什么。

使用 runBlocking 而不是 runBlockingTest 可以解决问题。