Skip to content

Odd failure message with SpyK and Coroutine #554

Open
@iainism

Description

@iainism

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy