kotlin - 如何做 livedata.postValue(any()) 的 coVerifyOrder - 它返回 io.mockk.MockKException:匹配模拟签名失败
问题描述
场景 - 嗨,我是使用 mockk 进行测试的新手。我想测试在视图模型中调用方法的顺序。我想livedata.postValue
在verify
block{} 中进行测试,但 mockk 给出了异常。还请帮助我理解异常的含义
我的视图模型.kt
fun doWork(showError: Boolean = false) {
launch {
val result = getImageUseCase.getImages()
if (!showError) {
withContext(uiDispatcher) {
liveDataResponse.postValue(LiveDataResult.success(Response(result)))
}
} else {
throw Exception("Unknown")
}
}
}
MyViewModelTest.kt
@Test
fun verifyOrderOfMethodExecution(){
coEvery { getImageUseCase.getImages() } returns 1
myViewModel.doWork()
coVerifyOrder {
getImageUseCase.getImages()
myViewModel.liveDataResponse.postValue(any())
}
}
例外 -
io.mockk.MockKException: Failed matching mocking signature for
SignedCall(retValue=, isRetValueMock=true, retType=class kotlin.Any, self=GetImageUseCase(usecase#1), method=getImages(Continuation), args=[Continuation at com.rahullohra.lab.MyViewModelTest$verifyOrderOfMethodExecution$2.invokeSuspend(MyViewModelTest.kt:79)], invocationStr=GetImageUseCase(usecase#1).getImages(continuation {}))
left matchers: [any()]
解决方案
推荐阅读
- r - R Shiny:从文件中渲染图像
- sql - How to pull/read data from an external SQL server in batches ard store all in one kdb+ table
- python - 在 Heroku 上部署 Flask 应用程序时,将 sqllite 数据库转换为 postgresql 数据库时出错
- google-sheets - 调整现有 Google 表格时间表模板上的脚本,以十进制格式显示总计
- mysql - 外部表到 MSSQL 2019 - 通过 ODBC 驱动程序从 MySQL
- linux - 无法在 Ubuntu 16.04.6 LTS 上安装 RDKit
- applescript - Q:Apple Script:如何获取IP地址
- javascript - html5 视频标签:是否可以限制全屏视频的大小?
- python - Python在数组的每个级别上创建具有条件的排列数组
- android - 在设置中获取电话名称 -> 关于手机 Android OS 选项