[英]Android Kotlin Unit test failing with io.mockk.MockKException: no answer found for
[英]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(從北極狐回滾)
我偶爾遇到的可能情況:
您正在驗證對象而不是模擬/間諜
在內部,onChanged 可能會命中內聯函數,或者在其他情況下是內聯函數本身
被請求的any()
對象有一個抽象類型參數。
MyObject<T>
與調用any<MyObject<T>>()
相同)解決方案
verify { mock.method(any<T>()) }
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.