0

I'm just trying to check if an user is created with an async method , I'm new to testing and I'm using ArgumentCaptor to check if the onRegistrationSucces() callback is called 1 time to check that it was succefull.

This is What I have done to test it

RegisterTest.kt

@Test fun should_SignUpUser(){ presenter.signUp("test1","[email protected]","asdasd") verify(interactor).createUserWithEmailAndPassword("test1","[email protected]","asdasd",object: RegisterInteractor.RegisterCallBack{ override fun onRegistrationSucces() { callbackCaptor.capture() } override fun onRegistrationFailure(errorMsg: String) { callbackCaptor.capture() } }) verify(callbackCaptor.value.onRegistrationSucces(), times(1)) } 

And this is the presenter method that I'm trying to test

RegisterPresenter.kt

override fun signUp(fullName:String, email: String, password: String) { view?.showProgress() registerInteractor.createUserWithEmailAndPassword(fullName,email, password, object : RegisterInteractor.RegisterCallBack { override fun onRegistrationSucces() { if(isViewAttached()){ view?.navigateToLogin() view?.hideProgress() } } override fun onRegistrationFailure(errorMsg:String) { if(isViewAttached()){ view?.showError(errorMsg) view?.hideProgress() } } }) } 

But I'm getting this error

Argument(s) are different! Wanted: interactor.createUserWithEmailAndPassword( "test1", "[email protected]", "asdasd", com.testapp.presentation.register.presenter.RegisterPresenterTest$should_SignUpUser$1@c86b9e3 ); -> at com.testapp.presentation.register.presenter.RegisterPresenterTest.should_SignUpUser(RegisterPresenterTest.kt:119) Actual invocation has different arguments: interactor.createUserWithEmailAndPassword( "test1", "[email protected]", "asdasd", com.testapp.presentation.register.presenter.RegisterPresenter$signUp$1@10aa41f2 ); -> at com.testapp.presentation.register.presenter.RegisterPresenter.signUp(RegisterPresenter.kt:64)

2 Answers 2

2
inner class below: class CallbackRegister extends RegisterInteractor.RegisterCallBack { private View view; private Object forViewattached; public CallbackRegister(Object forViewattached, View view){ this.forViewattached=forViewattached; this.view = view; } override fun onRegistrationSucces() { if(forViewattached.isViewAttached()){ view?.navigateToLogin() view?.hideProgress() } } override fun onRegistrationFailure(errorMsg:String) { if(forViewattached.isViewAttached()){ view?.showError(errorMsg) view?.hideProgress() } } //end class } Access this by the kotlin equivalent: object : new CallbackRegister(forViewattached, view) instead of object: RegisterInteractor.RegisterCallBack{...} you can easily test this by: TopLevelClass.CallbackRegister callbackRegisterUnderTest = new TopLevelClass().new CallbackRegister(MockforViewattached, MockView); You can now call the callback methods directly and verify the mocks: callbackRegisterUnderTest.onRegistrationSucces() verify the mocks did something. You have to convert this to kotlin, but I hope you see what is happening. 
Sign up to request clarification or add additional context in comments.

Comments

0

The issue is that the callbacks for each of the createuserwithemailandpasswords methods are different. This is why you see "argument is different" error.

Use the following to properly capture the callback:

verify(interactor).createUserWithEmailAndPassword(anyString(),anyString(),anyString(),callbackCaptor.capture()) verify(callbackCaptor.value.onRegistrationSucces(), times(1)) 

You can replace the anyString with unique string captors if you ever want to verify those.

5 Comments

yes, I did this before, but got stuck with the error callbackCaptor.capture() must not be null since some days , so thats why I change the implementation.
Definition of ArgumentCaptor @Captor private lateinit var callbackCaptor: ArgumentCaptor<RegisterInteractor.RegisterCallBack>
Next step, since I can't see all your code for initializing the interactors, is to capture one of the string values and verify it is not null and matches what you expect. If it doesn't match "test1", "[email protected]", or "asdasd", then there is an initialization issue with the interactor. Verify that next.
Also, you will not see callbackCaptor.value.onRegistrationSuccess called because it is not exercised in the code. to make the callbacks testable, I typically add them as a separate public class or as an inner class and call the override methods separately.
inner class below:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.