leosan
11/02/2021, 11:01 AMleosan
11/02/2021, 11:03 AMinternal class ViewModel @Inject constructor(
...
private val dispatcher: Dispatchers,
) : ViewModel() {
val listContent = MutableLiveData<PropertyGroupDisplayInfo>()
private var propertyChangedValue = MutableStateFlow("")
init {
viewModelScope.launchMain<PropertiesEditV2ViewModel> {
propertyChangedValue
.drop(1)
.debounce(DEBOUNCE_DELAY)
.mapNotNull(::validatePropertyInline)
.collect {
listContent.value = it
}
}
fun handleTextPropertyUpdate(property: String, value: String) {
viewModelScope.launch(dispatcher.default) { propertyChangedValue.emit(value) }
}
}
leosan
11/02/2021, 11:06 AMviewmodel
public method that is emitting a value to the flow
@Test
fun testNameHere() = runBlockingTest {
...
viewModel.run {
handleTextPropertyUpdate(Contact.EMAIL, "test@")
verify(validateEmailUseCase).invoke("test@")
}
}
leosan
11/02/2021, 11:08 AMTestDispatcher
in tests as well, debugging I can see that handleTextPropertyUpdate is called and the coroutine viewModelScope.launch(dispatcher.default) { propertyChangedValue.emit(value) }
too, but the flow never collects in tests not sure what is missingleosan
11/02/2021, 12:26 PMleosan
11/02/2021, 12:56 PMdebounce
operator it works… I can’t figure out why nwRobert Williams
11/02/2021, 5:14 PMadvanceTimeBy(DEBOUNCE_DELAY)
before the verify?leosan
11/02/2021, 5:45 PMRobert Williams
11/02/2021, 6:01 PMThread.sleep(DEBOUNCE_DELAY)
?leosan
11/03/2021, 9:16 AMdelay
in the runBlockingTest
block of the testRobert Williams
11/03/2021, 10:49 AMsleep
works but the advanceTimeBy
doesn't it's a problem with how you've set up the dispatchers. If neither works it's a problem with the flow setup