lundi 19 mars 2018

Observable.create is never called during tests when using subsribeOn

I have been working on this issue for some time now This is the code I am testing

public Observable<Void> prepare() {
        return Observable.<Void>create(subscriber -> {  
           // Some work is being done here but this part is never reached
            subscriber.onCompleted();
        }).subscribeOn(scheduler);
}

The scheduler is the looper's scheduler (the class inherits from HandlerThread)

The test I am writing is as follows:

@Test
public void prepare() throws Exception {
    TestSubscriber<Void> testSubscriber = new TestSubscriber<>();
    subject.start();
    Observable<Void> obs = subject.prepare();
    obs.subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(15, TimeUnit.SECONDS);
    //assertions go here
    subject.quit();
} 

The problem is the code in the Observable.create is never reached and the testSubscriber's timeout is always hit.
I've checked that the looper runs and that the scheduler exists. It works well when I run the app. I am using RobolectricTestRunner. Any ideas why the code is never reached?

Aucun commentaire:

Enregistrer un commentaire