GurpreetSK
04/21/2020, 4:19 PM@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.