我将使用Espresso测试 fragment,然后我想要模拟 viewmodels和成员。
在我的viewModel中,我有这样一个void function:
fun getLoginConfig() {
viewModelScope.launchApiWith(_loginConfigLiveData) {
repository.getLoginConfig()
}
}在测试fragment中,当我们从viewModel调用getLoginConfig()时,我希望用doNothing()来模拟,但是我用这个error面对:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed在testFragmentClass上的这一行:
@Before
fun setUp() {
//logOut
mockVm = mock(SplashVM::class.java)
loadKoinModules(module {
single {
mockVm
}
})
}
doNothing().`when`(mockVm.getLoginConfig()).let {
mockVm.loginConfigLiveData.postValue(Resource.Success(
LoginConfigResponse(
listOf("1"),1,1,"1",true)
))
}发布于 2021-11-02 14:32:18
有几件事:
doNothing只是什么也不做,这对于模拟上的void方法来说是不必要的。这是默认行为。如果您希望在模拟调用( doAnswer is the way to go.doVerb )语法中发生一些特定的事情,则莫基托希望那里只有一个变量;表达式不应该在模拟上调用方法,否则莫奇托认为您已经失去兴趣并抛出了UnfinishedStubbingException.。
因此,您的修补程序看起来如下:
doAnswer {
mockVm.loginConfigLiveData.postValue(Resource.Success(
LoginConfigResponse(
listOf("1"),1,1,"1",true)
))
}.`when`(mockVm).getLoginConfig()https://stackoverflow.com/questions/69808166
复制相似问题