[英]Kotlin Coroutine ViewModel UnitTesting does not cover viewmodel
I am facing issues in passing Coroutine UnitTestcase for ViewModel.我在为 ViewModel 传递协程 UnitTestcase 时遇到问题。 I am using MVVM retrofit.
我正在使用 MVVM retrofit。
Though I mocked the result, its displaying result as "null"虽然我嘲笑了结果,但它的显示结果为“null”
Please find below UnitTest case class:请在下面找到 UnitTest 案例 class:
val testDispatcher = TestCoroutineDispatcher()
@Test
fun `check viewmodel fetches data correctly`() = testDispatcher.runBlockingTest{
var retroRxModel = RetroRxModel("tile", "body", "1")
var retroRXModelList = ArrayList<RetroRxModel>()
retroRXModelList.add(retroRxModel)
response = Response.success(retroRXModelList)
retroCoroutineViewModel = RetroCoroutineViewModel(testDispatcher)
if (retrofit != null) {
if (apiService != null) {
Mockito.`when`(apiService.fetchUserPosts()).thenReturn(response)
}
}
retroCoroutineViewModel.fetchResponseFromAPI()
println("Loading Val::::${retroCoroutineViewModel.fetchLoadStatus()?.value}")
println("PostLive Dat::::${retroCoroutineViewModel.fetchPostLiveData().value}")
Assert.assertEquals(true,retroCoroutineViewModel.loading?.value)
}
Please find viewmodel method to be tested:请找到要测试的 viewmodel 方法:
fun fetchResponseFromAPI(){
viewModelScope.launch (dispatcher){
// postsMutableLiveData.postValue(null)
try{
val response = apiService.fetchUserPosts()
if(response.isSuccessful){
postsMutableLiveData.postValue(response.body())
loading.postValue(false)
// loading.value = false
}else{
loading.postValue(false)
errorOnAPI.postValue("Something went wrong::${response.message()}")
}
}catch (e:Exception){
loading.postValue(false)
errorOnAPI.postValue("Something went wrong::${e.localizedMessage}")
}
}
}
ViewModelFactory:视图模型工厂:
class RetroCoroutineViewModelFactory : ViewModelProvider.Factory {
@ExperimentalStdlibApi
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(RetroCoroutineViewModel::class.java)) {
return RetroCoroutineViewModel(Dispatchers.Main) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
When tried to run unittest, I see the control is returned after executing the below line in ViewModel without running through other code though data is mocked:当尝试运行 unittest 时,我看到在 ViewModel 中执行以下行后返回了控件,尽管数据被模拟,但没有运行其他代码:
val response = apiService.fetchUserPosts() val 响应 = apiService.fetchUserPosts()
Please help me in resolving this issue.请帮我解决这个问题。
I am using mockito framework我正在使用 mockito 框架
Because retroCoroutineViewModel
is not using the mocked apiService
instance.因为
retroCoroutineViewModel
没有使用模拟的apiService
实例。
The object of viewmodel
is being created and the apiService
is being mocked (considering the ifs are being executed) but the apiService
object in retroCoroutineViewModel
object is different than the mocked instance so you need to make sure that the retroCoroutineViewModel
object has the mocked apiService
object of your service (pass apiService
via constructor to RetroCoroutineViewModel?) The object of
viewmodel
is being created and the apiService
is being mocked (considering the ifs are being executed) but the apiService
object in retroCoroutineViewModel
object is different than the mocked instance so you need to make sure that the retroCoroutineViewModel
object has the mocked apiService
object of您的服务(通过构造函数将apiService
传递给 RetroCoroutineViewModel?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.