Test multiple Flows with `testIn`
trunkTo test multiple flows concurrently, use testIn to assign each Turbine to a separate variable. Unlike test, testIn does not automatically clean up its coroutine. To prevent tests from hanging, you should:
- Use
runTest'sbackgroundScopeto handle automatic cleanup. - Or, manually call
cancel(),awaitComplete(), orawaitError()before the scope ends.
ensureAllEventsConsumed() will be invoked when the calling coroutine completes.
runTest {
turbineScope {
val turbine1 = flowOf(1).testIn(backgroundScope)
val turbine2 = flowOf(2).testIn(backgroundScope)
assertEquals(1, turbine1.awaitItem())
assertEquals(2, turbine2.awaitItem())
turbine1.awaitComplete()
turbine2.awaitComplete()
}
}