I’m writing some tests that require a bit more fin...
# coroutines
j
I’m writing some tests that require a bit more fine grained control around advancing the dispatcher’s clock by using
runBlockingTest
but it seems to eagerly execute despite invoking
pauseDispatcher()
I tried out the sample provided on the kotlinx-coroutines-test documentation but still hitting similar problems. I’m wondering if there’s just something I’m overlooking here? Here’s what I’m running from the documentation sample:
Copy code
@Test
    fun testFooWithPauseDispatcher() = runBlockingTest {
        pauseDispatcher {
            foo() // <-- this is still eagerly executed
            // the coroutine started by foo has not run yet
            // runCurrent() // the coroutine started by foo advances to delay(1_000)
            // the coroutine started by foo has called println(1), and is suspended on delay(1_000)
            // advanceTimeBy(1_000) // progress time, this will cause the delay to resume
            // the coroutine started by foo has called println(2) and has completed here
        }
    }

    fun CoroutineScope.foo() {
        launch {
            println(1)   // executes after runCurrent() is called
            delay(1_000) // suspends until time is advanced by at least 1_000
            println(2)   // executes after advanceTimeBy(1_000)
        }
    }
a
Not exactly what you're looking for. But I use a single abstract use case that has a single suspend function in it. I call that use case from the ViewModel. Here's somethign I picked up after looking at the testing codelab. Might give you some ideas. https://github.com/alwarren/AndroidXMVVMDemo/blob/master/app/src/test/java/com/ntxdroid/spacex/feature/mission/MissionsViewModelTest.kt
j
Ahh my interpretation of the intended behavior was incorrect.
runBlockingTest
will drain the queue of pending operations after exiting the block, and that’s why I was seeing the output The links provided were helpful though, thanks for sharing!