https://kotlinlang.org logo
Title
j

juliocbcotta

05/14/2022, 8:43 PM
So, I am trying to write some tests for Flows using Turbine lib and I found myself in the need to assert some flow emissions that would be triggered after I call
.test
... something like
val flow = MutableStateFlow(1)
    flow.test {
        assertEquals(awaitItem(), 1)
        assertEquals(awaitItem(), 2) // <-- waits forever
}
flow.emit(2)
Would anyone have a tip on how to properly test it ?
p

Paul Woitaschek

05/14/2022, 9:15 PM
How would you expect this to work? You call the emit after you are waiting for the second item. Call emit between the two awaitItems
It's like expecting the lights to go on before pressing the switch
j

juliocbcotta

05/14/2022, 9:24 PM
I understand what is happening, I just don't know how to test it. I would like to listen to the emissions and make the emissions happen later in my test, that was just an example.
I would expect that turbine would make some "magic trick" inside the lambda to decouple the collection from the test.
p

Paul Woitaschek

05/14/2022, 9:38 PM
The trick is not to run the lamba. Iirc there was a call that gives you the Turbine directly
Once the lambda returns, Turbine is done. Depending on the use case just call your functions from within the lambda
j

jw

05/14/2022, 9:51 PM
Move emit inside the lambda
j

julian

05/17/2022, 1:23 AM
j

juliocbcotta

05/17/2022, 1:29 PM
Thanks, I read the doc, but missed the point of emitting inside the lambda as being the way to test that kind of case.
👍 1