Home > Blockchain >  Mockk verify ask a function to run inside a coroutine body
Mockk verify ask a function to run inside a coroutine body

Time:01-04

I was learning about Unit Testing with Mockk library, and the function i was testing is using a verify. So i run the test with coroutine test runTest{} but verify method ask me to run inside a coroutine body

Function Error

And, here is the code for function implementation :

override suspend fun getAnimeByAnimeId(animeID: Int): AnimeEntity? {
    return localDataSource.getAnimeByAnimeId(animeID)
}

So, i wonder what is wrong in here, why can't i call verify here even when the function is inside a coroutine body.

CodePudding user response:

You can use coVerify for verifying suspend functions with mockk. You have to make sure that you include the coroutines dependency in your tests (testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:x.x").

Check the mockk documentation on coroutines for more information.

  • Related