简体   繁体   中英

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. The testDispatcher is a TestCoroutineDispatcher() provided by the androidx testing library.

UPDATE / ANSWER

Thanks to the input of @Juan Cruy Soler I changed the way of injecting the database. 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. 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?

database should be mock or a spy to call verify on it.
I assume that it's not a mock because you are calling the method dataDao() in a previous line.

  • 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.
  • If you mock it, then you can remove this line database.dataDao().insert(listOf(DataDummies()))

You should use skyk to simulte a real scenario.

val database = spyk<YouDataBaseReference>()

Doing that, you'll able to use functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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