How to unit test that a `suspend fun` suspends unt...
# coroutines
t
How to unit test that a
suspend fun
suspends until a certain condition is met ? I tried the following :
Copy code
runBlocking() {
    val testSource = BehaviorProcessor.create<String>()
    val subjectUnderTest = MyCache(coroutineScope = this, source = testSource)

    val loading = async(start = CoroutineStart.UNDISPATCHED) { subjectUnderTest.getCachedValue() } // Suspend function call
    assertFalse(loading.isCompleted)
    
    // Sending an element to that processor satisfies the condition
    testSource.onNext("Foo")
    assertTrue(loading.isCompleted) // This fails : isCompleted is false
}
g
why do you need async? Why not just call function and check result?
t
Maybe it makes no sense to test that scenario, but I want to check that the suspending function resumes when an element is sent to the
BehaviorProcessor
it depends on. The only way I thought of was using async, as calling the suspending function may block the test indefinitely, so I wouldn't be able to satisfy the condition.