What's the correct way of testing shared flows? I ...
# android
l
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
I tried to use the Turbine library, but it throws an error saying the courroutine timed out
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
could you show some simple example where you getting time out with test?