question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Odd failure message with SpyK and Coroutine

See original GitHub issue

Prerequisites

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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
LachlanMcKeecommented, Jun 2, 2021

I assume this bug is still relevant? I don’t think it should be marked as stale

3reactions
fluxxion82commented, Aug 10, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange failure message (extra params) when verifying with ...
I do not understand the failure when using the spy. I am verifying getApiToken(any(), any()) (i.e. any() two times), while the failure message...
Read more >
mockk-io/Lobby - Gitter
Hi, I'm trying to use spyk on an Android ViewModel, but getting a weird issue for which I'm having trouble making sense. ``...
Read more >
[FREE] More Effective Coroutines | Page 11 - Unity Forum
I am restarting the game by calling `LoadScene`. `KillCoroutines()` is called on restart. `Assertion failed: WaitUntilDone cannot hold, the ...
Read more >
Exceptions in coroutines. Cancellation and Exceptions in…
On one hand, seeing an application crash is a bad experience for the user; on the other hand, showing the right message to...
Read more >
Update Logs | Typical Colors 2 Wiki - Fandom
Fixed not being able to apply the Taunt Kills Strange Part. ... If that failed aswell, you'll be kicked so ROBLOX doesn't corrupt...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found