Francesc
10/14/2023, 7:52 PMFrancesc
10/14/2023, 7:56 PMflow
because there are 2 flows being `combine`d
val query = MutableStateFlow("")
val sample: Flow<SomeObject> = query
.debounce(400L)
.distinctUntilChanged()
.flatMapLatest {
flow {
emit(State.Loading)
val response = interactor.getThing(params)
if (response.isSuccess) {
emit(state.Loaded(response.objects))
} else {
emit(state.Error(reason))
}
}
}
the fake implementation uses a Turbine
as shown here
class FakeGetThingInteractor : GetThingInteractor {
val things = Turbine<List<Thing>>()
suspend fun getThings(params: Params): List<Thing> {
return things.awaitItem()
}
}
when I test this flow, you'd expect Loading
and Loaded
, but Loading
is not received because it gets quickly replaced by Loaded
.
I have solved this by passing the backgroundScope.coroutineContext
to the fake as shown here
class FakeGetThingInteractor(
private val context: CoroutineContext,
) : GetThingInteractor {
val things = Turbine<List<Thing>>()
suspend fun getThings(params: Params): List<Thing> = withContext(context) {
things.awaitItem()
}
}
which works, but I wonder if there is a simpler solution. I also tried a yield
in the fake after the awaitItem
call, but that didn't helpPatrick Steiger
10/14/2023, 10:18 PMFrancesc
10/14/2023, 10:27 PM