简体   繁体   English

Mockk 验证要求 function 在协程体内运行

[英]Mockk verify ask a function to run inside a coroutine body

I was learning about Unit Testing with Mockk library, and the function i was testing is using a verify.我正在学习使用 Mockk 库进行单元测试,我正在测试的 function 正在使用验证。 So i run the test with coroutine test runTest{} but verify method ask me to run inside a coroutine body所以我用 coroutine test runTest{} 运行测试但是验证方法要求我在协程体内运行

函数错误

And, here is the code for function implementation:并且,这是 function 实现的代码:

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.所以,我想知道这里出了什么问题,为什么即使 function 在协程体内我也不能在这里调用验证。

You can use coVerify for verifying suspend functions with mockk.您可以使用coVerify来验证带有 mockk 的挂起函数。 You have to make sure that you include the coroutines dependency in your tests ( testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:xx" ).您必须确保在测试中包含协程依赖项 ( testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:xx" )。

Check the mockk documentation on coroutines for more information.查看有关协程的 mockk 文档以获取更多信息。

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

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