Egor Egorov
11/17/2020, 5:21 AMViewModel
and last point is MockWebServer
from OkHTTP using Coroutines
. In ViewModel I make a call using viewModelScope.launch
. However when I try to run this code
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
@Test
fun reloadExpense() = runBlocking {
viewModel.reloadExpense()
assertThat(viewModel.state.value, CoreMatchers.instanceOf(LCE.Content::class.java))
}
It does not wait for API request and just finishes the test. The MainCoroutine Rule is
@ExperimentalCoroutinesApi
class MainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
}
}
I have tried using runBlockingTest
,runBlocking
and different types of rules. Nothing seems to work. Thank you in advance!Michal Klimczak
11/17/2020, 8:07 AMEgor Egorov
11/17/2020, 7:59 PMMichal Klimczak
11/18/2020, 7:08 AMEgor Egorov
11/18/2020, 9:14 PMrunBlocking
on Retrofit suspend
callRafs
11/19/2020, 10:41 AMViewModel
and replace it with the TestCoroutineDispatcher
during tests else you will have have issues.