https://kotlinlang.org logo
#coroutines
Title
# coroutines
i

ilya-gh

05/20/2019, 12:49 PM
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

gildor

05/20/2019, 1:01 PM
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
4 Views