I'm trying to test a class that declares itself as a CoroutineScope
. The class has some methods that launch
jobs within its scope and I must assert the effects of those jobs from a test method.
Here's what I tried:
import kotlinx.coroutines.*
class Main : CoroutineScope {
override val coroutineContext get() = Job()
var value = 0
fun updateValue() {
this.launch {
delay(1000)
value = 1
}
}
}
fun main() {
val main = Main()
val mainJob = main.coroutineContext[Job]!!
main.updateValue()
runBlocking {
mainJob.children.forEach { it.join() }
}
require(main.value == 1)
}
My expectation was that updateValue()
will create a child of the root job in coroutineContext
. but it turns out that mainJob.children
is empty, so I can't await on the completion of launch
and the require
statement fails.
What is the proper way to make this work?