Maciek
01/08/2020, 11:58 AMjob
out of first coroutine launch and waiting for it in runBlocking
but that resulted in blocking the test indefinitelyDominaezzz
01/08/2020, 12:23 PM@Test
fun `test channels`() = runBlocking {
val channel = Channel<Int>()
launch {
assert(channel.receive() == 1)
}
launch {
delay(1)
channel.offer(0)
}
}
Maciek
01/08/2020, 12:32 PMMainScope
objects. This is just sample code to simulate architecture that I'm testing. I guess in order to make it testable I have to design code that scope can be injected and replaced in testsDominaezzz
01/08/2020, 12:45 PM@Test
fun `test channels`() {
val channel = Channel<Int>()
val job1 = MainScope().launch {
assert(channel.receive() == 1)
}
val job2 = MainScope().launch {
delay(1)
channel.offer(0)
}
runBlocking { job1.join(); job2.join() }
}
Maciek
01/08/2020, 12:48 PMrunBlocking
, I've changed the architecture to inject it in the objects constructors and everything works.
But I wonder if this case with separate scopes is testable at allDominaezzz
01/08/2020, 12:49 PMDispatchers.Main
might not be available in tests. Although I'm not sure if it matters.Maciek
01/08/2020, 12:50 PMDispatchers.setMain(TestCoroutineDispatcher())
so that shouldn't be a problem