Odd failure message with SpyK and Coroutine
See original GitHub issuePrerequisites
Please answer the following questions for yourself before submitting an issue.
- I am running the latest version
- I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
Also posted to: https://stackoverflow.com/questions/65492003/strange-failure-message-extra-params-when-verifying-with-spyk-and-coverify
Expected Behavior
Message on failing to verify spy matches actual function call
Current Behavior
What is the current behavior?
I have an integration test for an Android App where I am passing a spied Retrofit service (coroutine-based) to my repository:
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
}
This fails verification on the spy with the following message (note that the other tests pass, which means that the call was actually made!):
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
My corresponding unit test for the repository, using a mock, rather than a spy, behaves as expected:
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
}
I do not understand the failure message when using the spy. I am verifying getApiToken(any(), any())
(i.e. any()
two times), while the failure message refers to getApiToken(any(), any(), any()))
(i.e. any()
three times).
What have I done, that is making MockK try to verify the call on the spy with an additional parameter please?
Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
- MockK version: 1.10.4
- OS: Windows
- Kotlin version: 1.4.20
- JDK version: 1.8
- JUnit version: 1.12
- Type of test: unit test/integration test
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:17 (4 by maintainers)
Top GitHub Comments
I assume this bug is still relevant? I don’t think it should be marked as stale
I am also interested in this. I have a relaxed mocked variable of an interface, and if I explicitly make any kind of mock for the class then the coVerify will fail. If I don’t make a mock for it, then I guess the verify works.