Jan Skrasek
10/08/2020, 8:15 AMinterface Foo {
suspend fun test(i: Int)
}
@Test
fun myTest() = testCoroutineDispatcher.runBlockingTest {
val mutableState: MutableStateFlow<Int> = MutableStateFlow(1)
val foo = mockk<Foo>()
val job = launch {
mutableState
.filter { it != 1 }
.collect {
delay(100)
foo.test(it)
}
}
mutableState.value = 2
// how to wait here to be able to verify?
coVerify {
foo.test(2)
}
}
How to wait for the flow collection? Some part of the flow runs immediately and ultimately the execution is switched back to the test and assertion fails since the collection haven't run yet.bezrukov
10/08/2020, 8:26 AMval synchronizeJob = launch {
myClass.startSynchronize()
}
// your emits
synchronizeJob.join()
Jan Skrasek
10/08/2020, 9:27 AMbezrukov
10/08/2020, 10:14 AMtestCoroutineDispatcher.advanceUntilIdle()
after value update, and then cancel job.Jan Skrasek
10/08/2020, 10:28 AMJan Skrasek
10/08/2020, 1:58 PM