android - 如何在 Mockk Kotlin 中测试异步功能
问题描述
我想测试这个功能。
suspend fun fetchTwoDocs() =
coroutineScope {
val deferredOne = async { fetchDoc(1) }
val deferredTwo = async { fetchDoc(2) }
deferredOne.await()
deferredTwo.await()
}
如何在 mockk 中测试此功能
解决方案
如果你只是想测试这个函数,你可以简单地runBlocking
从你的测试中调用它,或者使用kotlinx-coroutines-test库,它提供runBlockingTest
:
@Test
fun test() = runBlocking {
val result = fetchTwoDocs()
// then assert stuff
}
推荐阅读
- c++ - g ++优化中断循环
- java - 如果年龄 > 0,如何编写 JSON 表达式以获取年龄值
- excel - 将 Excel 工作表导出到具有使用 VBA 选择路径和文件名的选项的 .csv 文件
- azure - 无法重定向到 Azure AD 登录
- angular - 刷新后保持选中的复选框
- .net - 如何将此列表限制为仅 AutoCAD 中的图纸空间布局?
- intellij-idea - 如何为 intellij 正确设置 adb/PATH
- typescript - 要联合的类数组(反之亦然)
- java - 当字符属性更改时,Java JTextPane 不会更新其大小
- bash - 使用 sudo 和 awk 进行 Bash while 循环