简体   繁体   English

Kotlin Mockk 错误:在 verify { ... } 块中缺少调用

[英]Kotlin Mockk Error: Missing calls inside verify { ... } block

I already read some issues with this or a similar error message (it is also ocurring for every {}), but none of them took me to a success result.我已经阅读了一些与此相关的问题或类似的错误消息(每个 {} 也会发生这种情况),但没有一个问题使我取得成功。

Any hints or suggestions on how to get this to work?有关如何使其工作的任何提示或建议?

Here is my setup and the Unit Test itself:这是我的设置和单元测试本身:

compileSdkVersion 29
defaultConfig {
   minSdkVersion 19
   targetSdkVersion 29
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

MockK version: 1.9.3.kotlin12
OS: Android
Kotlin version: 1.3.61
JDK version: jdk1.8.0_212
JUnit version: 4.12
Type of test: unit test

The stacktrace of the error:错误的堆栈跟踪:

io.mockk.MockKException: Missing calls inside verify { ... } block.

    at io.mockk.impl.recording.states.VerifyingState.checkMissingCalls(VerifyingState.kt:52)
    at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:21)
    at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
    at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
    at io.mockk.MockKDsl.internalVerify(API.kt:118)
    at io.mockk.MockKKt.verify(MockK.kt:139)
    at io.mockk.MockKKt.verify$default(MockK.kt:136)

My Unit Test:我的单元测试:

@Test
fun logout_clearsDatabase() {
    coroutineTestRule.testDispatcher.runBlockingTest {

        // ARRANGE
        database.dataDao().insert(listOf(DataDummies()))

        // ACT
        sut.logout()

        // ASSERT
        verify { database.clearAllTables() }
    }
}

I have no clue what I did wrong here.我不知道我在这里做错了什么。 database.clearAllTables() is a method provided by Room. database.clearAllTables()是 Room 提供的方法。 The testDispatcher is a TestCoroutineDispatcher() provided by the androidx testing library. testDispatcher是 androidx 测试库提供的TestCoroutineDispatcher()

UPDATE / ANSWER更新/答案

Thanks to the input of @Juan Cruy Soler I changed the way of injecting the database.感谢@Juan Cruy Soler 的输入,我改变了注入数据库的方式。 I did not return the real room database, instead I return a spy of it.我没有返回真实的房间数据库,而是返回了它的间谍。 That way the spy is injected into the SUT as well as into my test class.这样间谍就被注入到 SUT 以及我的测试类中。 After that change the test runs as excpected.更改后,测试按预期运行。 I this a feasible solution to my problem?我这是解决我问题的可行方法吗? Does it make sense to let the (Testing-)DependencyInjection create a spy?让 (Testing-)DependencyInjection 创建间谍有意义吗?

database should be mock or a spy to call verify on it. database应该是模拟或间谍来调用验证。
I assume that it's not a mock because you are calling the method dataDao() in a previous line.我认为这不是模拟,因为您在前一行调用了dataDao()方法。

  • If you don't want to mock it an use a real database instead, you should check that database.dataDao().getData() returns an empty list and remove the verify line.如果您不想模拟它而使用真实数据库,您应该检查database.dataDao().getData()返回一个空列表并删除verify行。
  • If you mock it, then you can remove this line database.dataDao().insert(listOf(DataDummies()))如果你嘲笑它,那么你可以删除这一行database.dataDao().insert(listOf(DataDummies()))

You should use skyk to simulte a real scenario.您应该使用 skyk 来模拟真实场景。

val database = spyk<YouDataBaseReference>()

Doing that, you'll able to use functions.这样做,您将能够使用函数。

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

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