class A(val coroutineContext: CoroutineContext = EmptyCoroutineContext) {
fun work(): String {
val result = runBlocking(coroutineContext) {
delay(3000)
return "some_result"
}
return result
}
}
@Test
fun `tests work`() {
val result = A().work()
assertEquals("some_result", result)
}
If a pass
TestCoroutineContext()
to runBlocking the
delay
is ignored as expected. However, It’s deprecated in favor of TestCoroutineScope.
TestCoroutineScope. However If I pass TestCoroutineScope the test never finishes:
Copy code
@Test
fun `tests work`() {
val result = A(TestCoroutineScope(TestCoroutineDispatcher()).coroutineContext).work()
// never reaches here
assertEquals("some_result", result)
}
What I am missing?
g
gildor
05/20/2019, 1:01 PM
Just use test coroutine context
gildor
05/20/2019, 1:02 PM
This way to get context of scope looks wrong in general, because you don't use scope and structured concurrency