muthuraj
08/04/2021, 7:58 AMFlow.debounce? The unit test doesn't wait until debounce is completed. It should actually advance the delay since I'm using runBlockingTest as per the documentation, but that doesn't happen. Here is an example that reproduces my case.
class DebounceTest(scope: CoroutineScope) {
private val stateFlow = MutableStateFlow("")
val resultFlow = MutableStateFlow("")
init {
stateFlow.debounce(500)
.onEach {
resultFlow.value = it
}
.launchIn(scope)
}
fun search(text: String){
stateFlow.value = text
}
}
@Test
fun debounceTest() = runBlockingTest {
val sut = DebounceTest(this)
sut.search("test")
assertThat(sut.resultFlow.value).isEqualTo("test")
}
The test here fails saying the actual value of resultFlow.value is empty string instead of testmateusz.kwiecinski
08/04/2021, 8:09 AMadvanceTimeBy(500) after sut.search("test")
runBlockingTest allows you to control the time but doesn’t do anything automatically during the test execution.muthuraj
08/04/2021, 8:39 AMadvancedtimeBy works, but I'm blocked by this issue
https://github.com/Kotlin/kotlinx.coroutines/issues/1531
I'm getting Test finished with active jobs error since the job inside the DebounceTest init block is not canceled.
And I can't cancel it manually since my actual code is Android ViewModel and it uses viewModelScope inside it.Nick Allen
08/04/2021, 6:00 PMviewModelScope creates its own scope (unless you create the TestCoroutineScope with viewModelScope). There's also a workaround in the issue. What's stopping you from just calling cancel on viewModelScope? If you are still stuck, maybe you could post a trivial example to show how you are setting up your unit test?muthuraj
08/04/2021, 6:05 PMrunBlockingTest and it worked fine after that.
https://stackoverflow.com/a/67897542