简体   繁体   English

MockK协程响应无法响应

[英]MockK Coroutine Response Fails to Answer

Trying to dig deeper into using MockK where a coroutine is involved.试图深入挖掘在涉及协程的情况下使用MockK I have this test:我有这个测试:

class UserDataUseCaseTest {

    @MockK
    val dataFetcherService: DataFetcherService = mockk()

    @MockK
    val userData: UserData = mockk()

    @ExperimentalCoroutinesApi
    @Test
    fun `fetching salt populates user data salt value`() = runBlockingTest {
        MockKAnnotations.init(this)
        val userDataUseCase = UserDataUseCase(mockk(), mockk(), mockk())
        every { userData.user_name } returns FAKE_USER
        coEvery { dataFetcherService.getSaltForUser(FAKE_USER) } returns SALT_RESPONSE
        userDataUseCase.getSaltForUser(userData)
        assertEquals(SALT, userData.salt)
    }

    companion object {
        private const val FAKE_USER = "fake_user"
        private const val SALT = "salt"
        private val SALT_RESPONSE = SaltResponse(
            result = "",
            title = "",
            message = "",
            salt = SALT,
            auth_code = "auth_code",
            userMustChangePassword = false
        )
    }
}

This fails with the following error:这失败并出现以下错误:

io.mockk.MockKException: no answer found for: DataFetcherService(#4).getSaltForUser(fake_user, continuation {}) io.mockk.MockKException:找不到答案:DataFetcherService(#4).getSaltForUser(fake_user, continuation {})

The method that I am trying to test looks like this:我尝试测试的方法如下所示:

@VisibleForTesting
suspend fun getSaltForUser(userData: UserData) {
    val saltResponse = dataFetcherService.getSaltForUser(userData.user_name)

    with (userData) {

        salt = if (saltResponse.salt.isEmpty())
            KeyChainWrapper().generateRandomString(KeyChainWrapper.SALT_LEN)
        else saltResponse.salt

        authCode = saltResponse.auth_code
        userMustChangePassword = saltResponse.userMustChangePassword
    }
}

Why am I not getting an answer for the getSaltForUser() call?为什么我没有得到getSaltForUser()调用的答案? Thanks for any help.谢谢你的帮助。

From the look of that sample code the class under test is not using the same mock as the one defined in the test:从该示例代码的外观来看,被测 class 没有使用与测试中定义的相同的模拟:

val userDataUseCase = UserDataUseCase(mockk(), mockk(), mockk())

should be something like应该是这样的

val userDataUseCase = UserDataUseCase(dataFetcherService, mockk(), mockk())

depending on which parameter the DataFetcherService should be取决于DataFetcherService应该是哪个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android kotlin 单元测试 - 线程“Test worker @coroutine#4”中的异常 - Android kotlin unit test - Exception in thread "Test worker @coroutine#4" io.mockk.MockKException: no answer found for: View(#1) Mockk 验证要求 function 在协程体内运行 - Mockk verify ask a function to run inside a coroutine body @MockK 或 mockk() - @MockK or mockk() 协程单元测试在该类中失败 - Coroutine Unit Testing Fails for the Class Kotlin 协程等待 Retrofit 响应 - Kotlin Coroutine wait for Retrofit Response 如果协程中存在“by injection{parametersOf(”someValue“)}”,则协程的单元测试失败 - Unit test fails for coroutine if “by inject{parametersOf(”someValue“)}” is present within coroutine Android Kotlin 单元测试因 io.mockk.MockKException 失败:找不到答案 - Android Kotlin Unit test failing with io.mockk.MockKException: no answer found for Android io.mockk.MockKException:找不到答案:日历(static Calendar#368的孩子).getTime() - Android io.mockk.MockKException: no answer found for: Calendar(child of static Calendar#368).getTime() 在 Kotlin 中让 Coroutine 等待 Jsoup 响应 - Make Coroutine wait for Jsoup response in Kotlin 如何在 Espresso 测试中等待 Coroutine 响应? - How to wait for Coroutine response in Espresso test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM