Conversation
gdaniels
left a comment
There was a problem hiding this comment.
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.
No real comment here, but that is quite a line of source code there. :)
| @Test | ||
| public void unbind_onlyOnce() throws Exception { | ||
| presenter.unbindView(); | ||
| verify(onUnbind, never()).run(); |
There was a problem hiding this comment.
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.
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.