sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

MVI-sample unit tests for presenters #197

Closed hussam789 closed 7 years ago

hussam789 commented 7 years ago

I was wondering if you want to build this sample using TDD, and probably start writing tests for MviPresenters, but currently MviBasePresenter has bindIntents function which

Observable<PartialHomeViewState> allIntentsObservable =
        Observable.merge(loadFirstPage, nextPage, pullToRefresh, loadMoreFromGroup)
            .observeOn(AndroidSchedulers.mainThread());

subscribeViewState(allIntentsObservable.scan(initialState, this::viewStateReducer),
        HomeView::render);

But if you want to write to test the MviPresenter, you want to test each Intent function e.g (loadFirstPage, pullToRefresh...). based on MVI pattern, given previousState (ViewModel) the Presenter applies the Model function (for example loadFirstPage) and uses StateReducer function to output a newState

MVI

sockeqwe commented 7 years ago
  1. In a real world app you wouldn't put all that things into your Presenter but rather in a HomeFeedInteractor which handles all those intents and applies the stateReducer. Maybe we should refactor that.
  2. I'm sorry, but I don't understand what exactly your question is / the purpose of this issue.
hussam789 commented 7 years ago

I understand, my question is could you provide an example for a unit test for the presenter in MVI sample app (currently there are no tests for it)? Or can you give us any advice for writing a test for MVI pattern in general?

sockeqwe commented 7 years ago

Got it! Yes that is not documented at all. I apologise. I will write add some unit tests for the sample app and will beginn to write documentation / github pages for MVI and Testing.

Basically, to test a MVI presenter you have to do the following: 1) Create a View Instance i.e. by using Mockito 2) Instantiate your presenter 3) call presenter.attachView(yourMockedView). This calls internally presenter.bindIntents() 4) now your presenter is setup properly and bound to the view. Now you can invoke the intent you want to test. 5) invoking an intent will finally result in a view.render(model). Then you catch this model and test it like Assert.assertEquals(expectedModel, receivedModel)

hussam789 notifications@github.com schrieb am Do., 26. Jan. 2017, 22:35:

I understand, my question is could you provide an example for a unit test for the presenter in MVI sample app (currently there are no tests for it)? Or can you give us any advice for writing a test for MVI pattern in general?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/sockeqwe/mosby/issues/197#issuecomment-275521236, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjnrthOoqvK0UWOBghXKDjkjZjUDdGvks5rWRGkgaJpZM4Luzh2 .

sockeqwe commented 7 years ago

Full example can be found here: https://github.com/sockeqwe/mosby/blob/master/sample-mvi/src/test/java/com/hannesdorfmann/mosby3/sample/mvi/view/home/HomePresenterTest.java

Will also put more infos about testing in documentation