I followed https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/ , looks like runBlockingTest is skipping the delay coroutine and I’m not able to reach the code after delay from main function. Is there a good way unit test/advance the timer and assert flags accordingly?
can you please help me to unit test my main function
// my main function is having multiple delay coroutines to set a flag true/false
fun delayFunction() {
launch { // coroutine scope
isFlag = false
delay(1000)
isFlag = true
delay(1000)
isFlag = false
}
}
// I tried unit testing something like this
@Test
fun `test delayFunction`() {
runBlockingTest {
delayFunction()
advanceTimeBy(1001)
assertThat(isFlag).isTrue()
advanceTimeBy(1001)
assertThat(isFlag).isFalse()
}
}