Eury Perez
04/24/2023, 12:54 PMval values = mutableListOf<Int>()
val sharedFlow = MutableSharedFlow<Int>()
sharedFlow.emit(0)
val job = launch { // <------
stateFlow.collect {
values.add(it)
}
}
runCurrent()
sharedFlow.emit(1)
runCurrent()
sharedFlow.emit(2)
runCurrent()
sharedFlow.emit(3)
runCurrent()
job.cancel()
assertEquals(listOf(0, 1, 2, 3), values)
A similar test passes with stateflowThiago Souto
04/24/2023, 1:03 PMTurbine
from CashApp
https://github.com/cashapp/turbineEury Perez
04/24/2023, 1:19 PMzsmb
04/24/2023, 1:38 PMSharedFlow
if there’s no cache configured you have to have the collector ready in time to collect it, otherwise you’ll just miss it.
Also, slightly unrelated: you can launch the collecting coroutine in backgroundScope to avoid having to keep a reference to its job and cancelling it manually at the end of the test.