简体   繁体   中英

io.mockk.MockKException: Failed matching mocking signature for SignedCall

I'm facing an issue with running mocked tests (using mockK & Kotlin).

I had a couple of working unit tests. Yesterday I was testing the feature I implemented. But whenever I try to run the tests, this exception appears:

io.mockk.MockKException: Failed matching mocking signature for
SignedCall(retValue=, isRetValueMock=true, retType=class kotlin.Unit, self=Observer(#1), method=onChanged(Any), args=[kotlin.Unit], invocationStr=Observer(#1).onChanged(kotlin.Unit))
left matchers: [any()]

    at io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:99)
    at io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:39)
    at io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:31)

Moreover, if I try to run the tests with coverage, android studio logs these errors:

[2021.08.24 12:05:30] (Coverage): Error during class instrumentation: kotlin.text.Regex: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 14
[2021.08.24 12:05:31] (Coverage): Error during class instrumentation: kotlin.text.StringsKt___StringsKt: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 14
[2021.08.24 12:05:31] (Coverage): Error during class instrumentation: kotlin.text.StringsKt__StringsKt: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 14
[2021.08.24 12:05:31] (Coverage): Error during class instrumentation: kotlin.text.StringsKt__StringsJVMKt: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 14
[2021.08.24 12:05:31] (Coverage): Error during class instrumentation: kotlin.text.StringsKt__StringNumberConversionsJVMKt: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 14

The code is just a simple call check, here's an example:

 @Test
    fun onLoginButtonPressed() {
        // Arrange
        val tObserver: Observer<Unit> = mockk(relaxUnitFun = true)
        viewModel.launchLoginScreenEvent.observeForever(tObserver)

        // Act
        viewModel.onLoginButtonPressed()

        // Assert
        verify(exactly = 1) { tObserver.onChanged(any()) }
    }

I don't know if these two problems are related, but they occurred together in my case.

Kotlin Version: 1.4.31 Android Studio: 4.2 (Rolled back from Arctic Fox)

Possible situations which occasionally I ran into:

  1. You're verifying an object instead of a mock/spy

  2. Internally onChanged might be hitting an inlined function or in other cases is the inlined function itself

  3. The any() object being requested has an Abstract Type argument.

  • (ie MyObject<T> which is the same as calling any<MyObject<T>>() )

Solutions

  1. Make sure to properly call a mock or spied object
  2. No way to solve
  3. If possible, adapt your production code to directly access T.
  • (ie verify { mock.method(any<T>()) } )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM