首页 > 解决方案 > 如何做 livedata.postValue(any()) 的 coVerifyOrder - 它返回 io.mockk.MockKException:匹配模拟签名失败

问题描述

场景 - 嗨,我是使用 mockk 进行测试的新手。我想测试在视图模型中调用方法的顺序。我想livedata.postValueverifyblock{} 中进行测试,但 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()]

标签: kotlinmockitoandroid-livedatamockk

解决方案


推荐阅读