i’m having trouble unit testing a flow. i have a
ViewModel
that converts a flow from my DB into a livedata for the view layer. the VM extends coroutine scope.
viewState.postValue(ViewState(loading = true))
this.launch {
repository.observeAll().collect { observedList ->
viewState.postValue(ViewState(list = observedList))
}
}
and then in my test I’d like to do something like this:
val expectedList = listOf(Thing())
coEvery { mockRepo.observeAll() } returns flow {
emit(expectedList)
}
viewModel.submitAction(Action.Refresh)
coVerify {
mockRepo.observeAll()
mockViewStateObserver.onChanged(ViewState(loading = true, list = listOf()))
mockViewStateObserver.onChanged(ViewState(loading = false, list = expectedList))
}
but the
coVerify
coroutine completes before the second update to the
LiveData
. anyone have suggestions besides injecting the test scope or delaying?