I’m trying to use the `sample` flow operator on a flow that I’m collecting on init of a class. When ...
j
I’m trying to use the
sample
flow operator on a flow that I’m collecting on init of a class. When I do this my unit tests on this class hang and never complete. I am setting the main Dispatcher to an
UnconfinedTestDispatcher
for my tests and injecting that main dispatcher into my class to use for it’s coroutine scope which has so far been sufficient, but
sample
seems to break this. Any ideas for how to work around this?
In case anyone else comes across this: I saw a couple Stack overflow posts about it, but no answers. I ended up resolving this by starting this flow within the backgroundScope provided by coroutine test, then the test is able to end as expected even though this flow is non-terminal.
d
I also just came up against this; it was most problematic where I want to run in the context of a job provided by a
viewModelScope
. I ended up exposing an optional
backgroundScope
construction parameter in my ViewModel to support testing, with a default of
null
that internal logic coalesces to the original
viewModelScope
, a bit 🤢 since I never like it when testing requirements impact the way I code for runtime, but it works.