This is my simple viewmodel
@HiltViewModel
class SplashViewModel @Inject constructor(
checkRemoteConfigUseCase: CheckRemoteConfigUseCase,
private val navigator: Navigator,
@EntryPoint(Feature.LOGIN) private val loginDestination: Destination,
) : ViewModel() {
private val _splashViewState: MutableStateFlow<SplashViewState> =
MutableStateFlow(SplashViewState.Loading)
val splashViewState = _splashViewState.asStateFlow()
init {
checkRemoteConfigUseCase()
.onSuccess {
when (it) {
RemoteConfigStatus.OK -> navigator.navigate(loginDestination) {
popUpTo(SplashGraph)
}
RemoteConfigStatus.NEED_UPDATE -> _splashViewState.value =
SplashViewState.UpdateNeeded
RemoteConfigStatus.DISABLED_APP -> _splashViewState.value =
SplashViewState.AppDisabled
}
}
.onFailure {
when (it) {
Error.Network -> _splashViewState.value = SplashViewState.NetworkError
else -> _splashViewState.value = SplashViewState.UnknownError
}
}
.launchIn(viewModelScope)
}
}
I want to test init
method so the test covers all the cases. I know how to handle all cases except for the one when RemoteConfigStatus
is OK
. The problem is that I can mock Navigator
and verify
that it is being called with loginDestination
but I do not know how to verify that the method is also called with popUpTo(SplashGraph)
as it is a lambda function that is inside my SUT.
This test method is obviously failing and not checking that navigator.navigate()
is called properly:
@Test
fun `init - when remote config is OK - navigates to login with popUpTo`() = runTest {
// Given
val remoteConfigStatus = RemoteConfigStatus.OK
whenever(checkRemoteConfigUseCase()).thenReturn(flowOf(Result.Success(remoteConfigStatus)))
// When
viewModel = SplashViewModel(checkRemoteConfigUseCase, navigator, loginDestination)
// Then
verify(navigator).navigate(
loginDestination
)
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744974194a4604045.html
评论列表(0条)