I am very new in Unit testing , i am trying to test my flow using Turbine library, it is not emitting all value, here is my test
fun `send function should emit Loading and Content states`() = runTest {
// Arrange
val userProfile = UserProfile(login = "test_login")
val contentState = UiState.Content(userProfile)
coEvery {
fakeRepository.getDetail(any())
} returns userProfile
// Act
viewModel.send(userProfile.login!!)
// Assert
testScheduler.advanceUntilIdle()
viewModel.uiState.test {
assertEquals(UiState.Loading, awaitItem())
assertEquals(contentState, awaitItem())
cancelAndIgnoreRemainingEvents()
}
}
and ViewModel is here, keep failing with reason
Expected UiState$Loading Actual :Content
viewModelScope.launch(dispatchers.main) {
flow {
emit(UiState.Loading)
val mResponse = userRepository.getDetail(login = login)
emit(UiState.Content(mResponse))
}.catch {
UiState.Error(it.message.toString())
it.printStackTrace()
}.flowOn(dispatchers.main).collect {
_uiState.value = it
}
}
Finally I got solution of my problem, I changed my dispatcher from UnConfinedTestDispatcher to StandardTestDispatcher, it starts working
standardtestdispatcher and unconfinedtestdispatcher
Here is my test code: