https://kotlinlang.org logo
#flow
Title
# flow
g

GurpreetSK

04/21/2020, 4:19 PM
Hi all. Hope you’re safe during this pandemic. I’m experimenting with flow and have a test where I need to push data into a stream once the entire flow is subscribed to. However, I’m not able to achieve this as the flow completes before the data is pushed into the channel. Attaching code snippet alongwith
Copy code
@InternalCoroutinesApi
    @Test
    fun `fetching trending repositories on screen launch fails`() {
        coEvery { repository.fetchMostPopularMovies() }
            .returns(
                flowOf<FetchEvent<List<TrendingRepository>>>(
                    FetchEvent(<http://FetchAction.IN|FetchAction.IN>_FLIGHT, null),
                    FetchEvent(
                        FetchAction.FETCH_FAILED,
                        null,
                        listOf(ApplicationError(ErrorType.NETWORK))
                    )
                )
            )
        val expectedStates = listOf(
            TrendingRepositoriesState(<http://FetchAction.IN|FetchAction.IN>_FLIGHT, emptyList()),
            TrendingRepositoriesState(
                FetchAction.FETCH_FAILED,
                emptyList(),
                ApplicationError(ErrorType.NETWORK)
            )
        )
        val actualStates = mutableListOf<TrendingRepositoriesState>()

        runBlocking {
            val job = GlobalScope.launch {
                TrendingRepositoriesModel
                    .bind(
                        lifecycle.asFlow(),
                        states.asFlow(),
                        intentions,
                        repository
                    )
                    .collect { actualStates.add(it) }
            }

            lifecycle.send(LifecycleEvent.CREATED)
            delay(500) // If not present, the items are always 0.
            job.cancel()
        }

        expectedStates.assertEqualTo(actualStates)
    }
Can someone please help here? Also, is there a better way of writing this test? I’m really missing the way RxJava tests read.