[Feature request] Attaching a view without calling `onAttachView()`
Opened this issue · 0 comments
I came across this while writing presenter tests with the help of Mockito.
Imaging you have the following code in your presenter:
@Override
protected void onAttachView(@NonNull final TiView view) {
super.onAttachView(view);
view.showLoading();
}
And you've written a test that verifies that showLoading()
was actually called on your view:
@Test
fun testAttachView_ShouldShowLoading() {
// When the View is attached to the Presenter.
presenterInstructor.attachView(view)
// Then the View should show a loading indicator.
then(view).should().showLoading()
}
Now you want to add a new method to the Presenter (for example onClick()
) that also should call view.showLoading()
and add a second test that verifies this behavior:
@Test
fun testOnClick_ShouldShowLoading() {
// When the View is attached to the Presenter.
presenterInstructor.attachView(view)
// And when the user clicks on the button.
presenter.onClick()
// Then the View should show a loading indicator.
then(view).should().showLoading()
}
Now comes the tricky part:
This test will pass even if the method onClick()
has an empty body!
How can this be?
Since ThirtyInch enforces you to attach the view to your Presenter before you can retrieve it (which is fine from a logical standpoint) it always calls onAttachView()
which causes your Presenters implementation to be executed.
Normally you want the scope of your test as small as necessary which means for the example of showLoading()
that you only want to verify what happens in onClick()
. But here is the issue with Mockito because the interactions with your mocks are recorded and every time you are done with your test setup (which includes attaching the view to the Presenter) onAttachView()
was called as well. This forces you to either verify that some method was called multiple times (which is not good in my opinion because it enlarges the scope of your test) or it enforces you to reset your mock (which is not so good as well because it can easily be missed to reset all mocks that already have interactions in onAttachView()
especially in a grown Presenter implementation).
And in case you are using RxTiPresenterSubscriptionHandler
to add a managed subscription in onAttachView()
it gets even more complicated to test.
Since we already use TiPresenterInstructor
in our tests to "instruct" the Presenter to attach a View. Wouldn't it be nice to have a way to bypass the onAttachView()
-callback if you don't need it in your test?
tl;dr:
I would love to see a way to setup the presenter for tests without onAttachView()
being called.