Hey guys, I am trying to mock `delay` in the follo...
# coroutines
i
Hey guys, I am trying to mock
delay
in the following code:
Copy code
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
Just use test coroutine context
This way to get context of scope looks wrong in general, because you don't use scope and structured concurrency