pdegand
05/24/2018, 10:21 AMsuspend fun start(channel: ReceiveChannel<Event>) {
launch(coroutineContext) {
for (event in channel) {
val view = // do something with each the event
withContext(context = UI) {
_evenementLiveData.value = view
}
}
}
}
and my test looks like this :
@Test
fun testStart() = runBlocking {
//given
val channel = ConflatedChannel<Event>()
val observer = mock<Observer<EvenementDetailView>>()
presenter.evenementLiveData.observeForever(observer)
val evenement = // create an event
//when
presenter.start(channel)
channel.send(event)
//then
assertThat(presenter.evenementLiveData.value).isEqualTo(expectedView)
}
However, as my method start is launching a coroutine, the assertion in the UT is run before the consumption of the event sent into the channel.
I tried to remove the launch { }
inside the start()
function, but without it, the runBlocking coroutine of the UT is suspended on presenter.start()
because of the for
loop inside the start()
function.
Am I doing something really wrong here in the start()
function or is this a bad design that is hard to unit test or am I simply missing a point somewhere ?
Thxgildor
05/24/2018, 11:27 AMpdegand
05/24/2018, 12:28 PMlaunch
in the suspend fun start()
function, and I wrapped the call to start()
in the unit test with launch(Unconfined)
and now it's working greatlouiscad
05/24/2018, 12:42 PMconsumeEach
or wrapping with consume
would do