使用 spyk 和 coVerify 验证时出现奇怪的失败消息(额外参数)
Strange failure message (extra params) when verifying with spyk and coVerify
我有一个集成测试,我正在将一个间谍 Retrofit 传递到我的存储库:
val apiSpy = spyk(PvApiService.getInstance())
val expectedTokenLength = 1290 // by definition
test("Token can be refreshed") {
val repo = Repository(apiSpy)
repo.reset()
repo.refreshToken() // Suspends, run on IO thread
coVerify (exactly = 1){apiSpy.tokenRetrofitService.getApiToken(any(), any()) }
repo.tokenAvailable shouldBe true
repo.token.length shouldBe expectedTokenLength
}
使用以下消息对间谍进行验证失败(请注意,其他测试通过,这意味着调用实际上已完成!):
Verification failed: call 2 of 2: PvApiTokenService(child of #2#3).getApiToken(any(), any(), any())) was not called
java.lang.AssertionError: Verification failed: call 2 of 2: PvApiTokenService(child of #2#3).getApiToken(any(), any(), any())) was not called
我对存储库的相应单元测试,使用模拟而不是间谍,表现符合预期:
val mockApi = mockk<PvApiService>(relaxed = true)
val testToken = "a token"
test("Token can be refreshed") {
coEvery { mockApi.tokenRetrofitService.getApiToken(any(), any()) } returns testToken
val repo = Repository(mockApi, ProjectConfig.testDispatcherProvider)
repo.refreshToken()
coVerify (exactly = 1){ mockApi.tokenRetrofitService.getApiToken(any(), any()) }
repo.token shouldBe testToken
repo.tokenAvailable shouldBe true
}
我不明白使用间谍时的失败。我正在验证 getApiToken(any(), any())
(即 any()
两次),而失败消息指的是 getApiToken(any(), any(), any()))
(即 any()
三次 次) .
我做了什么,让 MockK 尝试使用附加参数验证对间谍的调用?
编辑:我现在已经向 MockK 问题跟踪器添加了一个问题来尝试理解这种行为! https://github.com/mockk/mockk/issues/554
回答,此行为已被其他用户确认,现在被 MockK 项目作为错误跟踪:https://github.com/mockk/mockk/issues/554
我有一个集成测试,我正在将一个间谍 Retrofit 传递到我的存储库:
val apiSpy = spyk(PvApiService.getInstance())
val expectedTokenLength = 1290 // by definition
test("Token can be refreshed") {
val repo = Repository(apiSpy)
repo.reset()
repo.refreshToken() // Suspends, run on IO thread
coVerify (exactly = 1){apiSpy.tokenRetrofitService.getApiToken(any(), any()) }
repo.tokenAvailable shouldBe true
repo.token.length shouldBe expectedTokenLength
}
使用以下消息对间谍进行验证失败(请注意,其他测试通过,这意味着调用实际上已完成!):
Verification failed: call 2 of 2: PvApiTokenService(child of #2#3).getApiToken(any(), any(), any())) was not called
java.lang.AssertionError: Verification failed: call 2 of 2: PvApiTokenService(child of #2#3).getApiToken(any(), any(), any())) was not called
我对存储库的相应单元测试,使用模拟而不是间谍,表现符合预期:
val mockApi = mockk<PvApiService>(relaxed = true)
val testToken = "a token"
test("Token can be refreshed") {
coEvery { mockApi.tokenRetrofitService.getApiToken(any(), any()) } returns testToken
val repo = Repository(mockApi, ProjectConfig.testDispatcherProvider)
repo.refreshToken()
coVerify (exactly = 1){ mockApi.tokenRetrofitService.getApiToken(any(), any()) }
repo.token shouldBe testToken
repo.tokenAvailable shouldBe true
}
我不明白使用间谍时的失败。我正在验证 getApiToken(any(), any())
(即 any()
两次),而失败消息指的是 getApiToken(any(), any(), any()))
(即 any()
三次 次) .
我做了什么,让 MockK 尝试使用附加参数验证对间谍的调用?
编辑:我现在已经向 MockK 问题跟踪器添加了一个问题来尝试理解这种行为! https://github.com/mockk/mockk/issues/554
回答,此行为已被其他用户确认,现在被 MockK 项目作为错误跟踪:https://github.com/mockk/mockk/issues/554