Pick the fix
This test never reaches its assertion and fails with a runTest timeout. What's the fix?
@Test
fun showsLoadingThenData() = runTest {
val vm = FeedViewModel(load = { emptyList() })
val states = mutableListOf<FeedUiState>()
vm.state.collect { states += it }
vm.onRefresh()
assertEquals(false, states.last().isRefreshing)
}Hint 1 of 1
Does a StateFlow ever complete?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Collect in backgroundScope.launch { } or use Turbine's test { }
Correct. A StateFlow never completes, so collect must not run in the test body.
Why not the others?
- B Use runBlocking instead of runTest. Still hangs.
- C Add
delay(1000)after collect. That line is never reached. - D Call
first()twice. That gets one value each time, but doesn't test a sequence.