dan.the.man
08/05/2020, 3:27 PMcoroutinesTestRule.testDispatcher.runBlockingTest {
var latestNav: NavEvent? = null
navChannel.consumeEach {
latestNav = it }
vm.handleAction(NearbyListingClicked(nearbyListing))
assertEquals(latestNav, OpenListing(variant.listing!!.wmid.toString()))
}
But I'm just getting java.lang.IllegalStateException: This job has not completed yet
The Channel.consumeEach
appears to be blocking, which I don't think it should bestreetsofboston
08/05/2020, 3:29 PMnavChannel.consumeEach
is not blocking. It is likely suspending (difference there 🙂).
I don’t see all of the code, but if navChannel
is never closed (for sending), the consumeEach
just keeps being suspended….launch
a coroutine within your test-scope and that coroutine should send values to navChannel
and close it when needed.dan.the.man
08/05/2020, 3:31 PMnavChannel.consumeEach {
latestNav = it
navChannel.close()} }
So adding that block doesn't fix it, same problem, but wrapping it in a launch
block does fix it, which I understand is because the block is now on a different coroutine, and not blocking the teststreetsofboston
08/05/2020, 3:52 PMlatestNav = navChannel.receive()
dan.the.man
08/05/2020, 3:54 PMcoroutinesTestRule.testDispatcher.runBlockingTest {
val latestEvent = async { navChannel.receive() }
vm.handleAction(NearbyListingClicked(nearbyListing))
assertEquals(latestEvent.await(), OpenListing(variant.listing!!.wmid.toString()))
}
Ended up going with this and it's all working as expected, thanks!