RxPresso implements IdlingResource { public <T> With<T> given(Observable<T> observable) { RxMock mock = Observable.from(mocks).filter(provides(observable)).toBlocking().first(); final With<T> with = new With<>(mock, observable); pendingResources.add(with); with.registerIdleTransitionCallback( new ResourceCallback() { @Override public void onTransitionToIdle() { pendingResources.remove(with); if (pendingResources.isEmpty()) { resourceCallback.onTransitionToIdle(); } } } ); return with; } private  RxPresso(List<RxMock> mocks); static RxPresso from(Object... mocks); With<T> given(Observable<T> observable); @Override String getName(); @Override boolean isIdleNow(); @Override void registerIdleTransitionCallback(ResourceCallback resourceCallback); void resetMocks();  }
@Test public void itSendsEventsToMockedObservable() throws Exception { Observable<Integer> foo = mockedRepo.foo(3); rxPresso.given(foo) .withEventsFrom(SingleEvent.onNext(42)) .expect(any(Integer.class)); Integer result = foo.toBlocking().first(); assertThat(result).isEqualTo(42); }
@Test public void itSendsEventsToMockedObservableAccordingToParameter() throws Exception { Observable<Integer> foo = mockedRepo.foo(3); Observable<Integer> bar = mockedRepo.foo(1); rxPresso.given(foo) .withEventsFrom(SingleEvent.onNext(42)) .expect(any(Integer.class)); rxPresso.given(bar) .withEventsFrom(SingleEvent.onNext(24)) .expect(any(Integer.class)); Integer result = foo.toBlocking().first(); Integer result2 = bar.toBlocking().first(); assertThat(result).isEqualTo(42); assertThat(result2).isEqualTo(24); }
@Test public void itFailsIfNoEventMatchingMatcherIsReceived() throws Exception { expectedException.expectMessage("Expected Notification with kind OnNext but completed without matching"); Observable<Integer> foo = mockedRepo.foo(3); rxPresso.given(foo) .withEventsFrom(SingleEvent.<Integer>onCompleted()) .expect(any(Integer.class)); Integer result = foo.toBlocking().first(); assertThat(result).isEqualTo(42); }
@Test public void itFailsIfAnEventNotMatchingMatcherIsReceivedWhenUsingExpectOnly() throws Exception { expectedException.expectMessage("Expected Notification with kind OnNext but got"); Observable<Integer> foo = mockedRepo.foo(3); rxPresso.given(foo) .withEventsFrom(Observable.just(42)) .expectOnly(any(Integer.class)); Integer result = foo.toBlocking().first(); assertThat(result).isEqualTo(42); }