What's the correct way of testing shared flows? I have a shared flow that emits events to the UI (Show an error toast for example) but in the Vm's unit test that asserts the shared flow emission are stuck and keep running forever
Lucca Beurmann
12/25/2021, 6:38 PM
I tried to use the Turbine library, but it throws an error saying the courroutine timed out
Lucca Beurmann
12/25/2021, 6:42 PM
Foud the solution by moving the function that emits the shared flow inside the sharedFlow.test block:
Copy code
@Test
fun `Should show error toast when get more photos fails`() = runBlockingTest {
coEvery {
getAuthorPhotoUseCase(
any(),
any()
)
} returns flow<Source<List<AuthorPhotos>>> {
emit(
Source.Error(
NetworkError(
"",
500
)
)
)
}
viewModel.events.test {
viewModel.getMoreAuthorPhotos()
coVerify(exactly = 1) { getAuthorPhotoUseCase(any(), any()) }
confirmVerified(getAuthorPhotoUseCase)
val emission = awaitItem()
assertTrue(emission is AuthorDetailsEvents.ShowErrorToast)
cancelAndIgnoreRemainingEvents()
}
}
Is this correct or just a workaround?
g
gildor
12/27/2021, 4:24 AM
could you show some simple example where you getting time out with test?