Tim Malseed
12/30/2021, 10:32 PMrunBlockingTest()
, this delay is skipped, so this function completes immediately, and the StateFlow essentially bypasses the value I’m trying to test.Tim Malseed
12/30/2021, 10:33 PMsealed class ViewState {
object None : ViewState()
object Loading : ViewState()
object Some : ViewState()
}
class Foo {
val viewState = MutableStateFlow<ViewState>(ViewState.None)
fun updateViewState() {
viewState.value = ViewState.Loading
viewState.value = someLongRunningOperation()
}
private suspend fun somePotentiallyLongRunningOperation(): ViewState {
delay(5000)
return ViewState.Some
}
}
@Test
fun myTest() = runBlockingTest {
foo.viewState.test {
foo.updateViewState()
assertThat(awaitItem()).isInstanceOf(ViewState.Loading::class.java) // Fails. ViewState is ViewState.Some
cancelAndConsumeRemainingEvents()
}
}
Paul Woitaschek
12/30/2021, 10:42 PMTim Malseed
12/30/2021, 10:45 PMTim Malseed
12/30/2021, 11:29 PMrunTest()
does seem to solve most of my problems, and revealed a few new ones that make me wonder how the tests ever passed at all!Paul Woitaschek
12/31/2021, 9:10 AM