You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’m trying to test my ViewModel State, with StateFlow and Mockk.
`private val _state = MutableStateFlow(HomeState())
val state = _state.asStateFlow()
fun sendEvent(homeEvents: HomeEvents) = when (homeEvents) {
HomeEvents.FetchProducts -> fetchAvailableProducts()
}
private fun fetchAvailableProducts() {
viewModelScope.launch(coroutineDispatcher) {
_state.update { it.copy(isLoading = true) }
when (val result = verifyProductsAvailableUseCase.invoke()) {
is Result.Success -> {
_state.update {
it.copy(products =
result.data.sortedBy { product -> product.match.not() })
}
}
is Result.Error -> {
}
}
}
}`
But in my test, the job is collecting only the initial value and the last value, ignoring the second update with param loading true.
What can be wrong on this test ?
@Test fun given call init should update state correctly`() =
runTest {
val mockProducts = BaseUnitTestSupportData.mockCardProductsPreApproved
Please use Kotlin Slack or StackOverflow for general questions.
Your question is difficult to read because of the problems with formatting, and the full test code (including the initialization code) is not provided, so I can't say anything for sure, but I'd guess that the issue is with the fact that fetchAvailableProducts launches a coroutine, the coroutine gets scheduled for execution, but does not execute until later. I'd try adding advanceUntilIdle after sendEvent.
Alternatively, maybe using the Turbine library to test the flow would help.
I’m trying to test my ViewModel State, with StateFlow and Mockk.
`private val _state = MutableStateFlow(HomeState())
val state = _state.asStateFlow()
But in my test, the job is collecting only the initial value and the last value, ignoring the second update with param loading true.
What can be wrong on this test ?
@Test fun
given call init should update state correctly`() =runTest {
val mockProducts = BaseUnitTestSupportData.mockCardProductsPreApproved
The text was updated successfully, but these errors were encountered: