samedi 3 février 2018

Rxjava testing verify methods invocation at onNext

I have ViewModel from Arch component. the view model have method called registerUser the method body is RxJava operation and after the subscribe called successfully i post the value to the activity using live data and call two other methods

public LiveData<User> registerUser(HashMap<String, String> userInfo, boolean isLogin) {
    Observable<User> auth = isLogin ? userRepository.loginUser(userInfo) : userRepository.registerUser(userInfo);
    compositeDisposable.add(auth
            .subscribeOn(Schedulers.io())
            .subscribe(user -> {
                registerUser.postValue(user);
                //save user
                if (user.getError().equals("0")) {
                    userRepository.setUserToken(user.getAccessToken());
                    userRepository.setRegisterUser(user);
                }
            }, throwable -> {
                Timber.e(throwable.getMessage());
                registerUser.postValue(null);
            }));
    return registerUser;
}

I am testing this function and i want to make sure that the two methods are called like this

 @Test
    public void registerUser_Login() {
        when(userRepository.loginUser(anyStringHashMap())).thenReturn(Observable.just(TEST_USER));
        userViewModel.registerUser(anyStringHashMap(), true);
        verify(userRepository).loginUser(anyStringHashMap());

        //the two methods i want to check
        verify(userRepository).setRegisterUser(TEST_USER);
        verify(userRepository).setUserToken(TEST_USER.getAccessToken());
    }

But the test fails and the two methods doesn't get called how to fix this problem ?

Aucun commentaire:

Enregistrer un commentaire