我在一个方法中有 2 个可完成项,例如:
completable1.doSomething()
.andThen(completable2.doSomethingElse())
我想通过使用 Mockito 并在第一个可完成项中返回错误来测试它。这是我原来的方法:
public Completable updateQuestion(Question question){
return gameRepositoryType.updateQuestion(question)
.andThen(gameRepositoryType.getGameById(question.getqId())
.map(GAME_MAPPER)
.flatMapCompletable(gameRepositoryType::updateGame))
.observeOn(AndroidSchedulers.mainThread());
}
这是我的测试:
@Test
public void updateQuestionError(){
when(gameRepositoryType.updateQuestion(question)).thenReturn(Completable.error(error));
when(question.getqId()).thenReturn(FAKE_QID);
when(gameRepositoryType.getGameById(FAKE_QID)).thenReturn(Single.just(game));
when(gameRepositoryType.updateGame(game)).thenReturn(Completable.complete());
interactor.updateQuestion(question)
.test()
.assertNotComplete()
.assertError(error);
verify(gameRepositoryType).updateQuestion(question); //this is called in test
verify(question,never()).getqId(); //this is called in test
verify(gameRepositoryType,never()).getGameById(FAKE_QID); //this is called in test
verify(gameRepositoryType,never()).updateGame(game); //this is NEVER called in test
}
在测试方法结束时的验证中,我期望在第一个可完成的错误发出后,最后 3 个验证应该成功运行,即最后 3 个 verify()
应该永远不会叫做。
但是我可以看到所有方法都被调用,即使 gameRepositoryType.updateQuestion(question)
发出错误。这是为什么?
andThen()
如果第一个 completable 发出错误,难道不应该被调用吗?
最佳答案
completable1.doSomething()
.andThen(completable2.doSomethingElse())
Shouldn't andThen() never be called if the first completable emitts error?
andThen
将始终调用 completable2.doSomethingElse()
来构建流,但它返回的 Completable 永远不会被订阅。
你可以这样测试:
when(completable1.doSomething()).thenReturn(Completable.error(error));
CompletableSubject completableSubject = CompletableSubject.create();
when(completable2.doSomethingElse()).thenReturn(completableSubject);
TestObserver testObserver = completable1.doSomething()
.andThen(completable2.doSomethingElse()).test();
// Verify that completable2.doSomethingElse() was never subscribed to:
assertFalse(completableSubject.hasObservers());
testObserver
.assertError(error)
.assertNotComplete();
编辑:进一步说明:
鉴于您的方法定义如下:
private Completable updateQuestion(Question question) {
System.out.println("prints when the updateQuestion is called");
return Completable.fromAction(() -> {
System.out.println("prints when updateQuestion is subscribed to");
database.updateQuestion(question);
});
}
private Completable updateGame() {
System.out.println("prints when the updateGame is called");
return Completable.fromAction(() -> {
System.out.println("prints when updateGame is subscribed to");
database.updateGame();
});
}
假设你调用:
updateQuestion(question)
.andThen(updateGame())
.subscribe();
database.updateQuestion(question);
抛出一个错误。
那么你的日志输出将是:
prints when the updateQuestion is called
prints when the updateGame is called
prints when updateQuestion is subscribed to
>> error
和database.updateGame();
永远不会被执行
关于android - RxJava2 : . andThen() 被调用,即使第一个 Completable 发出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56516502/