请在下面找到一个使用协程替换回调的函数:
override suspend fun signUp(authentication: Authentication): AuthenticationError {
return suspendCancellableCoroutine {
auth.createUserWithEmailAndPassword(authentication.email, authentication.password)
.addOnCompleteListener(activityLifeCycleService.getActivity()) { task ->
if (task.isSuccessful) {
it.resume(AuthenticationError.SignUpSuccess)
} else {
Log.w(this.javaClass.name, "createUserWithEmail:failure", task.exception)
it.resume(AuthenticationError.SignUpFail)
}
}
}
}
现在我想对这个功能进行单元测试。我正在使用 Mockk:
@Test
fun `signup() must be delegated to createUserWithEmailAndPassword()`() = runBlockingTest {
val listener = slot<OnCompleteListener<AuthResult>>()
val authentication = mockk<Authentication> {
every { email } returns "email"
every { password } returns "pswd"
}
val task = mockk<Task<AuthResult>> {
every { isSuccessful } returns true
}
every { auth.createUserWithEmailAndPassword("email", "pswd") } returns
mockk {
every { addOnCompleteListener(activity, capture(listener)) } returns mockk()
}
service.signUp(authentication)
listener.captured.onComplete(task)
}
不幸的是,由于以下异常,此测试失败了: java.lang.IllegalStateException: This job has not completed yet
我试图用 runBlockingTest
runBlocking
但测试似乎在无限循环中等待。
有人可以帮我解决这个 UT 吗?
提前致谢
原文由 suns9 发布,翻译遵循 CC BY-SA 4.0 许可协议
从 这篇 文章可以看出:
而不是执行此操作:
创建一个测试范围通过测试调度程序:
然后在测试中执行
testScope.runBlockingTest
另请参阅 Craig Russell 的 “使用 TestCoroutineDispatcher 对协程暂停函数进行单元测试”