-
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.intrepid.skeleton.base; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
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; | ||
|
||
@Mock | ||
private Runnable onBind; | ||
|
||
@Mock | ||
private Runnable onUnbind; | ||
|
||
private Disposable disposable; | ||
|
||
@Before | ||
public void setUp() { | ||
presenter = new TestPresenter(view, testConfiguration); | ||
} | ||
|
||
@Test | ||
public void bind_onlyOnce() throws Exception { | ||
presenter.bindView(view); | ||
presenter.bindView(view); | ||
assertEquals(view, presenter.view); | ||
verify(onBind, times(1)).run(); | ||
} | ||
|
||
@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 commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this again, I'd actually split this into two tests, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
presenter.bindView(view); | ||
presenter.unbindView(); | ||
presenter.unbindView(); | ||
verify(onUnbind, times(1)).run(); | ||
} | ||
|
||
@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() { | ||
onBind.run(); | ||
disposable = Observable.never().subscribe(); | ||
disposables.add(disposable); | ||
} | ||
|
||
@Override | ||
protected void onViewUnbound() { | ||
onUnbind.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.
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 😄