我正在尝试为使用Observable
的简单类编写测试。
必须为函数buildUseCaseObservable
编写测试,该函数应首先尝试从网络获取数据,如果不成功则尝试从本地数据库获取。
在buildUseCaseObservable
我使用运算符first
,它应该过滤数据,如果数据不为空且为空则返回true。
如果在rest.getData()
的情况下调用并且返回的数据不为null,我认为first
应该返回true,在这种情况下data.getData()
不应该被调用。
但在我的情况下,似乎没有调用测试函数first
,并且始终调用函数rest.getData()
和data.getData()
。
我做错了什么以及如何纠正测试?
@PerActivity
public class DataInteractor extends Interactor {
private RestService rest;
private DataService data;
@Inject
DataInteractor(RestService rest, DataService data) {
this.rest = rest;
this.data = data;
}
@Override
protected Observable buildUseCaseObservable() {
return Observable.concat(
rest.getData(),
data.getData())
.first(data -> data != null && !data.isEmpty());
}
}
public interface DataService {
Observable<List<IData>> getData();
}
public interface RestService {
@GET("data")
Observable<List<IData>> getData();
}
public class DataInteractorTest {
private DataInteractor interactor;
@Mock private RestService mockedRest;
@Mock private DataService mockedData;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.interactor = new DataInteractor(mockedRest, mockedData);
}
@Test
public void firstDownloadDataFromNetwork() {
when(mockedRest.getData()).thenReturn(Observable.create(new Observable.OnSubscribe<List<IData>>() {
@Override
public void call(Subscriber<? super List<IData>> subscriber)
List<IData> data = new ArrayList<IData>() {{
add(new Data());
}};
subscriber.onNext(data);
subscriber.onCompleted();
}
}));
this.interactor.buildUseCaseObservable()
verify(this.mockedData, times(0)).getData();
}
}
答案 0 :(得分:0)
幸运的是,我找到了测试Rx内容的解决方案和正确的方法。 我找到了一个名为RxAssertions的nice article非常helpful class,对我的测试的一个小修改开始传递。
public class DataInteractorTest {
private DataInteractor interactor;
@Mock private RestService mockedRest;
@Mock private DataService mockedData;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.interactor = new DataInteractor(mockedRest, mockedData);
}
@Test
@SuppressWarnings("unchecked")
public void downloadDataFromNetwork_ignoreDataFromDatabase() {
when(mockedRest.getData()).thenReturn(this.getMockedData(4));
when(mockedData.getData()).thenReturn(this.getMockedData(8));
RxAssertions.subscribeAssertingThat(this.interactor.buildUseCaseObservable())
.completesSuccessfully()
.hasSize(4);
}
/**
* Helper function which return mocked data
*/
private Observable getMockedData(final int size) {
return Observable.create(new Observable.OnSubscribe<List<IData>>() {
@Override
public void call(Subscriber<? super List<IData>> subscriber) {
List<IData> data = new ArrayList<>();
for (int i = 0; i < size; ++i) {
data.add(new Data());
}
subscriber.onNext(data);
subscriber.onCompleted();
}
});
}
}
public class RxAssertions {
public static <T> ObservableAssertions<T> subscribeAssertingThat(Observable<List<T>> observable) {
return new ObservableAssertions<>(observable);
}
public static class ObservableAssertions<T> {
private List<T> mResult;
private Throwable mError;
private boolean mCompleted;
public ObservableAssertions(Observable<List<T>> observable) {
mCompleted = false;
mResult = new ArrayList<>();
observable.subscribeOn(Schedulers.immediate())
.subscribe(new Observer<List<T>>() {
@Override
public void onCompleted() {
mCompleted = true;
}
@Override
public void onError(Throwable error) {
mError = error;
}
@Override
public void onNext(List<T> list) {
mResult.addAll(list);
}
});
}
public ObservableAssertions<T> completesSuccessfully() {
if (!mCompleted || mError != null) {
if (mError != null) mError.printStackTrace();
throw new AssertionFailedError("Observable has not completed successfully - cause: "
+ (mError != null ? mError : "onComplete not called"));
}
return this;
}
public ObservableAssertions<T> fails() {
if (mError == null) {
throw new AssertionFailedError("Observable has not failed");
}
return this;
}
public ObservableAssertions<T> failsWithError(Throwable throwable) {
fails();
if (!throwable.equals(mError)) {
throw new AssertionFailedError("Observable has failed with a different error," +
" expected is " + throwable + " but thrown was " + mError);
}
return this;
}
public ObservableAssertions<T> hasSize(int numItemsExpected) {
if (numItemsExpected != mResult.size()) {
throw new AssertionFailedError("Observable has emitted " + mResult.size()
+ " items but expected was " + numItemsExpected);
}
return this;
}
@SafeVarargs
public final ObservableAssertions<T> emits(T... itemsExpected) {
completesSuccessfully();
assertEmittedEquals(itemsExpected);
return this;
}
@SuppressWarnings("unchecked")
public ObservableAssertions<T> emits(Collection<T> itemsExpected) {
completesSuccessfully();
assertEmittedEquals((T[]) itemsExpected.toArray());
return this;
}
public ObservableAssertions<T> emitsNothing() {
completesSuccessfully();
if (mResult.size() > 0) {
throw new AssertionFailedError("Observable has emitted " + mResult.size() + " items");
}
return this;
}
private void assertEmittedEquals(T[] itemsExpected) {
hasSize(itemsExpected.length);
for (int i = 0; i < itemsExpected.length; i++) {
T expected = itemsExpected[i];
T actual = mResult.get(i);
if (!expected.equals(actual)) {
throw new AssertionFailedError("Emitted item in position " + i + " does not match," +
" expected " + expected + " actual " + actual);
}
}
}
}
}