Unit Test - Verify Observable is subscribed

前端 未结 4 2105
攒了一身酷
攒了一身酷 2021-02-08 00:19

I have got the java code like this

 mDataManager.getObservable(\"hello\").subscribe( subscriber );

and I want to verify the follow

4条回答
  •  悲哀的现实
    2021-02-08 01:03

    I found that RxJava provides a class called TestSubject

    You can create it like this

    private TestScheduler eventsScheduler = new TestScheduler();
    private TestSubject eventObservable = TestSubject.create(eventsScheduler);
    

    This will provide you with the method hasObservers() which returns a boolean.

    @Test
    public void testSubscription(){
        myTestClass.init(eventObservable);
    
        assertTrue(eventObservable.hasObservers());
    }
    

    Also the TestSubject allows you to perfectly time when events should be sent.

    eventObservable.onNext(new MyEvent());
    eventsScheduler.triggerActions(); 
    

提交回复
热议问题