MockK - 左匹配器的模拟签名匹配失败:[any(), any()]

问题描述 投票:0回答:5

我想实施一些 UI 测试以确保今天实施的代码明天可以使用,但是当试图查看过去实施的 UI 测试是否有效时,它会抛出此错误:

Caused by: io.mockk.MockKException: Failed matching mocking signature for left matchers: [any(), any()]

这发生在

every {} return Unit
行上,该行有一个名为 WakeUpTimeManagerobject 文件,调用一个 .set(param1, param2) 函数并且 在该函数中有一些内联函数 我认为它可能导致问题,但我不知道。我尝试在互联网上搜索但找不到解决方案。

这是抛出错误的测试:

  @Before
  fun setup() {
    mockkObject(WakeUpTimerManager)
    every { WakeUpTimerManager.set(any(), any()) } returns Unit
  }

这是调用

every
行的函数

  fun set(context: Context, timer: Timer) {
    if (timer.atMillis < System.currentTimeMillis()) {
      return
    }

    if (Preset.findByID(context, timer.presetID) == null) {
      return
    }

    //This is an inline function
    withGson {
      PreferenceManager.getDefaultSharedPreferences(context).edit {
        putString(PREF_WAKE_UP_TIMER, it.toJson(timer))
      }
    }

    //This is an inline function
    withAlarmManager(context) {
      it.setAlarmClock(
        AlarmManager.AlarmClockInfo(timer.atMillis, getPendingIntentForActivity(context)),
        getPendingIntentForService(context, timer)
      )
    }
  }

问题: 为什么mockk会抛出这个错误?这是怎么回事?有什么解决办法吗?

android kotlin android-testing mockk
5个回答
7
投票

尝试

mockkStatic(WakeUpTimerManager::class)
。对我来说
mockkObject
也没有用,但是
mockkStatic
做了


6
投票

在我的例子中,我使用了错误的注释来模拟依赖关系。

我使用

@MockBean
org.springframework.boot.test.mock.mockito.MockBean
,而我应该使用
@MockkBean
com.ninjasquad.springmockk.MockkBean


1
投票

在我的例子中,我使用了

any()
的类型转换。我想测试
viewModel.show(Message())
调用的方法。但是这个方法重载了(具有不同类型的签名),所以我尝试将参数
any()
转换为
Message
.

// show is overloaded method
fun show(resourceId: Int) {}
fun show(text: String) {}
fun show(message: Message) {}

// But it threw the exception.
verify { viewModel.show(any() as Message) }

// This won't work because Message() object will be different 
verify { viewModel.show(Message()) }

也许嘲笑

message
会有所帮助,但对我来说不是。

// val message = mockk<Message>()
// every { Message() } returns message
// verify { viewModel.show(message) }

我不得不添加

mockkStatic
,因为我使用了扩展方法。例如,
fun ViewExtension.show()

mockkStatic(ViewExtension::class.java.name + "Kt") // Like "com.example...ViewExtensionKt"

然后模拟一个行为

every { viewModel.show(Message()) } just Runs
verify { viewModel.show(any() as Message) }

1
投票

有时,尤其是对于 Dagger Hilt 和用 Mockk 模拟替换对象实例的全局测试模块,并不完全清楚是使用模拟对象还是真实对象。对我来说正是这样——我缺少依赖项,所以我的真实实例没有被模拟实例替换,所以 mockk 回答了这个非常奇怪的错误:

io.mockk.MockKException: Failed matching mocking signature for

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)
    at io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:50)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:63)
    at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
    at io.mockk.MockKDsl.internalVerify(API.kt:119)
    at io.mockk.MockKKt.verify(MockK.kt:149)
    at io.mockk.MockKKt.verify$default(MockK.kt:140)

0
投票

在我的例子中,我使用

模拟了一个 Companion 对象方法
mockkStatic(ObjectName::class)
every { ObjectName.method() } returns blah

诀窍是什么

mockkObject(ObjectName)

来源-https://github.com/mockk/mockk/issues/136

© www.soinside.com 2019 - 2024. All rights reserved.