簡體   English   中英

io.mockk.MockKException:SignedCall 的匹配模擬簽名失敗

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

我在運行模擬測試時遇到了問題(使用 mockK 和 Kotlin)。

我有幾個工作單元測試。 昨天我正在測試我實現的功能。 但是每當我嘗試運行測試時,就會出現這個異常:

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)

此外,如果我嘗試使用覆蓋率運行測試,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

代碼只是一個簡單的調用檢查,這里是一個例子:

 @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()) }
    }

我不知道這兩個問題是否相關,但在我的案例中它們是一起發生的。

Kotlin 版本:1.4.31 Android Studio:4.2(從北極狐回滾)

我偶爾遇到的可能情況:

  1. 您正在驗證對象而不是模擬/間諜

  2. 在內部,onChanged 可能會命中內聯函數,或者在其他情況下是內聯函數本身

  3. 被請求的any()對象有一個抽象類型參數。

  • (即MyObject<T>與調用any<MyObject<T>>()相同)

解決方案

  1. 確保正確調用模擬或間諜對象
  2. 沒有辦法解決
  3. 如果可能,調整您的生產代碼以直接訪問 T。
  • (即verify { mock.method(any<T>()) }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM