Hi, i recently updated to to newest coroutine vers...
# coroutines
t
Hi, i recently updated to to newest coroutine version (1.6.0) and i am now experiencing some strange behaviour when it comes to unit testing using turbine. While the following minimal example is working fine with the now deprecated
TestCoroutineDispatcher
, it fails (
TimeoutCancellationException
) with the new coroutines version and the
UnconfinedTestDispatcher
.. any ideas how this issue can be resolved?
Copy code
// ViewModel
    val loading = MutableStateFlow(false)

    fun onReloadClicked() {
        viewModelScope.launch {
            loading.value = true
            // load data
            loading.value = false
        }
    }
Copy code
// 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())
        }
    }
d
Please see https://github.com/cashapp/turbine/issues/42#issuecomment-1000317026. Looks like it has the same root cause, and the workaround may be applicable.