mardi 29 novembre 2016

Testing RxJava Subscribers in Android

I am writing an Android app adhering to the MVP design pattern and using RxJava to make network calls.

My Presenter layer is responsible for making a network call (via an injected network layer object) and then setting the state of the view before and after the network call (i.e. show a progress bar then hide the progress bar).

The Presenter contains a Subscriber and because of that I'm wrestling with the best way to write unit tests for the Presenter class.

As I see it there are two options:

  1. One test class will assert that the Presenter interacts with the view correctly before the Observable is subscribed to. A different test class would be used to test just the Subscriber and would assert that the view is interacted with correctly after the network call completes. In other words, one test class would test the Presenter and another test class would test the Subscriber and the Presenter test would test that things behaved correctly pre-subscription and the Subscriber test would test that things behaved correctly after post-subscription.

  2. A single test class is used that asserts that the view is interacted with correctly before the Observable is subscribed to and then the Observable is mocked so that it can return different responses (onError(), onNext(), etc...) and the test can verify that the view was interacted with correctly when the Observable is subscribed to. In this case there is just one test class that tests everything.

I like option 1 because it separates testing the subscriber separately from the presenter which "feels" right. However I like option 2 because once you've written your test class you're done and you've covered every case.

Are there better ways to do this than I've suggested? Any arguments for option 1 vs option 2 vs a completely different way?

Aucun commentaire:

Enregistrer un commentaire