Picture the situation in an MVP pattern where your presenter subscribes to a service returning an observer:
public void gatherData(){
service.doSomeMagic()
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new TheSubscriber());
}
Now the class TheSubscriber
calls onNext
a method from the view, say:
@Override public void onNext(ReturnValue value) {
view.displayWhatever(value);
}
Now, in my unit test I would like to verify that when the method gatherData()
is called on a non-erroneous situation, the view's method displayWhatever(value)
is called.
The question:
Is there a clean way to do this?
Background:
- I'm using mockito to verify the interactions and a lot more of course
- Dagger is injecting the entire presenter except for
TheSubscriber
What have I tried:
- Inject the subscriber and mock it in the tests. Looks a bit dirty to me, because if I want to change the way the presenter interacts with the service (Say not Rx) then I need to change a lot of tests and code.
- Mock the entire service. This was not so bad, but requires me to mock a lot of methods and I didn't quite reach what I wanted.
- Looked up around the internet, but no one seems to have a clean straight way of doing this
Thanks for the help
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…