-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test case for BasePresenter #54
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this better with Mockito.spy()
-
package io.intrepid.skeleton.base;
import android.support.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import io.intrepid.skeleton.testutils.PresenterTestBase;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
public class BasePresenterTest extends PresenterTestBase<BasePresenter<BaseContract.View>> {
@Mock
private BaseContract.View view;
private TestPresenter spyPresenter;
private Disposable disposable;
@Before
public void setUp() {
spyPresenter = Mockito.spy(new TestPresenter(view, testConfiguration));
presenter = spyPresenter;
}
@Test
public void bind_onlyOnce() throws Exception {
presenter.bindView(view);
presenter.bindView(view);
assertEquals(view, presenter.view);
verify(spyPresenter, times(1)).onViewBound();
}
@Test
public void unbind_onlyOnce() throws Exception {
presenter.unbindView();
verify(spyPresenter, never()).onViewUnbound();
presenter.bindView(view);
presenter.unbindView();
presenter.unbindView();
verify(spyPresenter, times(1)).onViewUnbound();
}
@Test
public void unbind_clearsDisposables() {
presenter.bindView(view);
assertFalse(disposable.isDisposed());
presenter.unbindView();
assertTrue(disposable.isDisposed());
}
private class TestPresenter extends BasePresenter<BaseContract.View> {
TestPresenter(@NonNull BaseContract.View view,
@NonNull PresenterConfiguration configuration) {
super(view, configuration);
}
@Override
protected void onViewBound() {
disposable = Observable.never().subscribe();
disposables.add(disposable);
}
}
}
WDYT?
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class BasePresenterTest extends PresenterTestBase<BasePresenter<BaseContract.View>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No real comment here, but that is quite a line of source code there. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming is hard 😄
@Test | ||
public void unbind_onlyOnce() throws Exception { | ||
presenter.unbindView(); | ||
verify(onUnbind, never()).run(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this again, I'd actually split this into two tests, presenterWithoutViewDoesNotCallOnViewUnbound()
(or whatever) and unbind_onlyOnce()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good call. I was bouncing around a bit in how I split these up since there's a bit of duplication due to lifecycle necessity, but this can definitely be split in two.
To recap some in-person conversation RE Glen's point above, I recently had some issues on Motiv (cc @streetsofboston) where we were having issues with breakpoints in our tests when using |
Heh, using spy() upped the coverage by one line, since we call into the real |
@gdaniels This has been sitting for a while. Any more thoughts? |
No description provided.