This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the possibility to expect/expectOnly for finite/infinite pipelines
- Loading branch information
Showing
5 changed files
with
199 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,5 @@ buildscript { | |
allprojects { | ||
repositories { | ||
jcenter() | ||
maven { | ||
url 'https://dl.bintray.com/novoda/maven/' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
core/src/test/java/com/novoda/rxpresso/RxPressoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.novoda.rxpresso; | ||
|
||
import android.support.test.espresso.IdlingResource; | ||
|
||
import com.novoda.rxmocks.RxMatcher; | ||
import com.novoda.rxmocks.RxMocks; | ||
import com.novoda.rxmocks.SimpleEvents; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import rx.Notification; | ||
import rx.Observable; | ||
|
||
import static com.novoda.rxmocks.RxExpect.any; | ||
import static org.fest.assertions.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class RxPressoTest { | ||
|
||
public @Rule ExpectedException expectedException = ExpectedException.none(); | ||
|
||
private TestRepository mockedRepo; | ||
private RxPresso rxPresso; | ||
private IdlingResource.ResourceCallback resourceCallback; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
mockedRepo = RxMocks.mock(TestRepository.class); | ||
resourceCallback = mock(IdlingResource.ResourceCallback.class); | ||
rxPresso = new RxPresso(mockedRepo); | ||
rxPresso.registerIdleTransitionCallback(resourceCallback); | ||
} | ||
|
||
@Test | ||
public void itSendsEventsToMockedObservable() throws Exception { | ||
Observable<Integer> foo = mockedRepo.foo(3); | ||
|
||
rxPresso.given(foo) | ||
.withEventsFrom(SimpleEvents.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(SimpleEvents.onNext(42)) | ||
.expect(any(Integer.class)); | ||
rxPresso.given(bar) | ||
.withEventsFrom(SimpleEvents.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 resetMocksResetsPipelines() throws Exception { | ||
Observable<Integer> foo = mockedRepo.foo(3); | ||
|
||
rxPresso.given(foo) | ||
.withEventsFrom(SimpleEvents.onNext(42)) | ||
.expect(any(Integer.class)); | ||
|
||
rxPresso.resetMocks(); | ||
|
||
Observable<Integer> bar = mockedRepo.foo(3); | ||
|
||
rxPresso.given(bar) | ||
.withEventsFrom(SimpleEvents.<Integer>onCompleted()) | ||
.expect( | ||
new RxMatcher<Notification<Integer>>() { | ||
@Override | ||
public boolean matches(Notification<Integer> actual) { | ||
return actual.getKind() == Notification.Kind.OnCompleted; | ||
} | ||
|
||
@Override | ||
public String description() { | ||
return "Completed event"; | ||
} | ||
} | ||
); | ||
|
||
Boolean result = bar.isEmpty().toBlocking().first(); | ||
|
||
assertThat(result).isTrue(); | ||
} | ||
|
||
@Test | ||
public void idlingRessourceTransitionsToIdleWhenDataIsDelivered() throws Exception { | ||
Observable<Integer> foo = mockedRepo.foo(3); | ||
|
||
rxPresso.given(foo) | ||
.withEventsFrom(SimpleEvents.onNext(42)) | ||
.expect(any(Integer.class)); | ||
|
||
assertThat(rxPresso.isIdleNow()).isFalse(); | ||
|
||
Integer result = foo.toBlocking().first(); | ||
|
||
assertThat(rxPresso.isIdleNow()).isTrue(); | ||
} | ||
|
||
@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(SimpleEvents.<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); | ||
} | ||
|
||
public interface TestRepository { | ||
Observable<Integer> foo(int bar); | ||
} | ||
|
||
} |