Henning B
10/21/2021, 12:44 PMprivate var _state: State by mutableStateOf(Loading)
override val state: State = _state
fun onLoadClicked() {
_state = Loading
viewModelScope.launch {
_state = when (val content: Content? = loadData()) {
null -> Error
else -> PresentData(content)
}
}
}
So now I would like to test the behaviour of onLoadClicked. But I do not know how to verify that calling that function sets the state to Loading and than to PresentData/Error. I could observe a liveData and remember all states is was set to but how do I do it with mutableStateOf?maciejciemiega
10/21/2021, 12:55 PMrunBlockingTest { }
and use pauseDispatcher()
(then it will not go through launch { }
immediately, so you can test the Loading
state. Then invoke advanceUntilIdle()
and check the state after that.Henning B
10/21/2021, 1:00 PMHenning B
10/21/2021, 2:04 PMHenning B
10/21/2021, 2:05 PMrunBlockingTest(testDispatcher) {
pauseDispatcher()
sut.onReloadClick()
sut.state.shouldBeTypeOf<Loading>()
advanceUntilIdle()
sut.state.shouldBeTypeOf<PresentData>()
}
Albert Chang
10/21/2021, 2:09 PMoverride var state: State by mutableStateOf(Loading)
private set