Thomas Kranzer
03/31/2022, 10:41 AMTestCoroutineDispatcher
, it fails (TimeoutCancellationException
) with the new coroutines version and the UnconfinedTestDispatcher
.. any ideas how this issue can be resolved?
// ViewModel
val loading = MutableStateFlow(false)
fun onReloadClicked() {
viewModelScope.launch {
loading.value = true
// load data
loading.value = false
}
}
// Test Code
private val testDispatcher = UnconfinedTestDispatcher()
@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
}
@After
fun tearDown() {
Dispatchers.resetMain()
}
@Test
fun testLoading() = runTest(testDispatcher) {
viewModel.loading.test {
assertFalse(awaitItem())
viewModel.onReloadClicked()
assertTrue(awaitItem()) // fails with TimeoutCancellationException
assertFalse(awaitItem())
}
}
Dmitry Khalanskiy [JB]
04/01/2022, 12:44 PM