https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
a

Aidan Low

06/21/2022, 8:56 PM
How can I best use turbine to verify the eventual value of a StateFlow? If I use
awaitItem()
I seem to see each individual time the StateFlow was set, but what I care about is the eventual value after a number of operations. It manifests most painfully in my combine-heavy view model with a lot of
.stateIn()
operations but can also be seen in
Copy code
val stateFlow = MutableStateFlow(false)
stateFlow.test {
  stateFlow.value = true
  val a = awaitItem() // this is false
  val b = awaitItem() // this is true
}
t

Trevor Stone

06/21/2022, 8:58 PM
Is the eventual value unique compared to the intermediate ones? If so, and your test is to ensure the flow reaches that state, you can to
stateFlow.filter{}.test{ awaitItem() }
where you use filter to remove the noise that isn't important for the test
a

Aidan Low

06/21/2022, 9:00 PM
It's not necessarily unique. In this case it's deciding whether to show some Android UI based on a number of other flows (some mutablestateflows, some database-driven) so it might stay at its initial value of false or might change to true.
e

ephemient

06/21/2022, 9:01 PM
how long is "eventual"?
you could
Copy code
delay(100)
expectMostRecentItem()
with caveats about timing
a

Aidan Low

06/21/2022, 9:02 PM
They're the result of a combine operation across a number of other flows (up to 8 in some cases) so it's a short period of time but potentially a number of updates.
33 Views