简体   繁体   English

io.mockk.MockKException:SignedCall 的匹配模拟签名失败

[英]io.mockk.MockKException: Failed matching mocking signature for SignedCall

I'm facing an issue with running mocked tests (using mockK & Kotlin).我在运行模拟测试时遇到了问题(使用 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:此外,如果我尝试使用覆盖率运行测试,android studio 会记录以下错误:

[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) Kotlin 版本:1.4.31 Android Studio:4.2(从北极狐回滚)

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在内部,onChanged 可能会命中内联函数,或者在其他情况下是内联函数本身

  3. The any() object being requested has an Abstract Type argument.被请求的any()对象有一个抽象类型参数。

  • (ie MyObject<T> which is the same as calling any<MyObject<T>>() ) (即MyObject<T>与调用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.如果可能,调整您的生产代码以直接访问 T。
  • (ie verify { mock.method(any<T>()) } ) (即verify { mock.method(any<T>()) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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