android - Verify that a mocked object has been called with an specific lambda argument - Stack Overflow

This is my simple viewmodel@HiltViewModelclass SplashViewModel @Inject constructor(checkRemoteConfigU

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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信