dead.fish
11/22/2022, 12:39 PMsuspend
function that feeds a StateFlow
with one or two different values, in quick succession. How would I test this with Coroutines 1.6? Issue is that during the test I only see the second emitted value and the first is already gone. I tried different things using different dispatches, but nothing worked out unfortunately.Dmitry Khalanskiy [JB]
11/22/2022, 12:49 PMStateFlow
in an UnconfinedTestDispatcher
. This will work unless the emissions themselves happen in an unconfined dispatcher.
However, could you share why you set SharedFlow
to two values in quick succession? In production, the first value will typically be lost as well, because SharedFlow
conflates values. If you rely on both elements being received in production, this is very tricky to actually ensure.dead.fish
11/22/2022, 2:22 PMdead.fish
11/22/2022, 2:26 PMdead.fish
11/22/2022, 2:26 PMDmitry Khalanskiy [JB]
11/22/2022, 2:28 PMIn my test of course I don’t have such delaysThe recommended solution is to mock the network call with
delay(20.seconds)
. In the test framework, delays are performed using a virtual source of time. So, in fact, the delay will finish instantly, but the test will "think" that 20 seconds passed.dead.fish
11/22/2022, 2:30 PMdead.fish
11/22/2022, 2:30 PMCompletableDeferred
solution for this, which did not work)dead.fish
11/22/2022, 2:36 PMCasey Brooks
11/22/2022, 3:34 PM