带有实时数据的单元测试始终在android.os.oper中获取错误&quot getmainlooper。
在我的ViewModel中,我使用实时数据持有响应状态。在这里,代码:
@HiltViewModel
class MainViewModel @Inject constructor(private val repository: Repository): ViewModel() {
private val _charData = MutableLiveData<Response<List<Character>>>()
val charData: LiveData<Response<List<Character>>>
get() = _charData
init {
getCharacters()
}
fun getCharacters(){
viewModelScope.launch {
_charData.value = repository.getCharacters()
}
}
}
我的测试总是会因该错误而失败,并且问题在行_CHARDATA.VALUE = repository.getCharacters()
中。我还查看了相同的问题,应该通过添加@get:unial val instantexecutorrule:instanttaskexecutorrule = instanttaskexecutorrule()
来处理。但是,错误仍然存在。在这里我的测试代码:
@RunWith(JUnit4::class)
class MainViewModelTest {
@get:Rule
val mainRule = MainCoroutineRule()
lateinit var repository: Repository
lateinit var viewModel: MainViewModel
@get:Rule
val instantExecutorRule: InstantTaskExecutorRule = InstantTaskExecutorRule()
@Before
fun setUp(){
MockitoAnnotations.openMocks(this)
repository = MockRepository()
viewModel = MainViewModel(repository)
}
@Test
fun getCharactersSuccess() = runTest {
val observer = mock<Observer<Response<List<Character>>>>()
viewModel.getCharacters()
viewModel.charData.observeForever(observer)
assertTrue(viewModel.charData.value is Response.Success)
}
}
我应该如何解决此错误? 谢谢
In my ViewModel, I use live data to hold the response state. Here the code:
@HiltViewModel
class MainViewModel @Inject constructor(private val repository: Repository): ViewModel() {
private val _charData = MutableLiveData<Response<List<Character>>>()
val charData: LiveData<Response<List<Character>>>
get() = _charData
init {
getCharacters()
}
fun getCharacters(){
viewModelScope.launch {
_charData.value = repository.getCharacters()
}
}
}
My test always fails with that error and the problem is in the line _charData.value = repository.getCharacters()
. I also have looked at the same problem and it should be handled by adding @get:Rule val instantExecutorRule: InstantTaskExecutorRule = InstantTaskExecutorRule()
. However, the error still persists. Here my test code:
@RunWith(JUnit4::class)
class MainViewModelTest {
@get:Rule
val mainRule = MainCoroutineRule()
lateinit var repository: Repository
lateinit var viewModel: MainViewModel
@get:Rule
val instantExecutorRule: InstantTaskExecutorRule = InstantTaskExecutorRule()
@Before
fun setUp(){
MockitoAnnotations.openMocks(this)
repository = MockRepository()
viewModel = MainViewModel(repository)
}
@Test
fun getCharactersSuccess() = runTest {
val observer = mock<Observer<Response<List<Character>>>>()
viewModel.getCharacters()
viewModel.charData.observeForever(observer)
assertTrue(viewModel.charData.value is Response.Success)
}
}
How should I fix this error?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
add
我找到了解决方案。
您可以使用robolectric。
@runwith(MockitoJunitRunner :: class)
在您的ViewModel test类中替换I find the solution.
You can use Robolectric.
Replace
@RunWith(MockitoJUnitRunner::class)
with in your ViewModel Test class@RunWith(RobolectricTestRunner::class)
Also use the below dependency for rorolectric