tseisel
09/01/2019, 7:14 PMclass MyClass(private val scope: CoroutineScope) {
// launches new coroutines in the passed scope
}
Some of my test scenarios requires that the passed CoroutineScope
be cancelled.
How would you write such test with runBlockingTest
?Evan R.
09/03/2019, 12:37 PMtseisel
09/03/2019, 4:47 PMfun TestCoroutineScope.runInScope(block: suspend CoroutineScope.() -> Unit) {
val job = launch(block = block)
advanceUntilIdle()
job.cancel()
}
The passed block
is executing eagerly due to runBlockingTest
, advances until idle then cancel the scope. This should be enough to simulate the lifecycle of a child CoroutineScope
.
I wonder if such thing is possible with existing constructs, for example coroutineScope { ...}
.