Hi. I'm trying to understand basics of testing hot Flows when using `runTest`. Have a look at the t...
g
Hi. I'm trying to understand basics of testing hot Flows when using
runTest
. Have a look at the two tests below. The first uses runBlockingTest, the other runTest.
Copy code
@Test
fun `test shared flow with deferred`() = runBlockingTest {
    val sharedFlow = MutableSharedFlow<Int>(replay = 0)
    val deferred = async { sharedFlow.first() }
    sharedFlow.emit(1)
    assertEquals(1, deferred.await())
}

@Test
fun `test shared flow with deferred - runTest`() = runTest {
    val sharedFlow = MutableSharedFlow<Int>(replay = 0)
    val deferred = async { sharedFlow.first() }
    sharedFlow.emit(1)
    assertEquals(1, deferred.await())
}
Why the second one never finish?
e
the contents of
async {}
will not have run yet in `runTest`; use
runCurrent()
between it and
.emit()
or
async(start = CoroutineStart.UNDISPATCHED)
may work as well
👍 1