Testing Shared flows with lambda functions #328
Replies: 1 comment
-
Turbine gives you the ability to receive these lambda instances as they are emitted. That's its only job. So ultimately this becomes about how you would test these lambdas if you received them some other way? In general, since you cannot meaningfully compare lambdas, the general approach to testing lambda instances (which is not at all specific to Turbine) is to invoke them and have their implementation do something that you can observe. For example, val items = ArrayList<String>()
flow.test {
yourThing.emit({ items += "one" })
yourThing.emit({ items += "two" })
awaitItem().invoke()
assertThat(items).containsExactly("one")
awaitItem().invoke()
assertThat(items).containsExactly("one", "two")
} Of course I have no idea where your lambdas are coming from or how you are triggering emissions and so on. But the principle is the same: invoke the lambda, assert on some side-effect that it causes to happen. |
Beta Was this translation helpful? Give feedback.
-
I have a
val flow: SharedFlow<(Object) -> Unit> that emits a lambda function
How can use turbine to test this one?
Beta Was this translation helpful? Give feedback.
All reactions