に、私はユースケースオブジェクトをテストしたい、この特定のケースでは、このようになりますLoginUseCaseが、そこにある:UntTest - モックRxJavaオブジェクトユースケースオブジェクト
public class LoginUseCase implements RxUseCase<AuthResponse, AuthCredentials> {
ApiManager mApiManager;
public LoginUseCase(ApiManager apiManager) {
mApiManager =apiManager;
}
@Override
public Observable<AuthResponse> execute(final AuthCredentials authCredentials) {
return Observable.just(1)
.delay(750, TimeUnit.MILLISECONDS)
.flatMap(l -> mApiManager.login(authCredentials.getLogin(), authCredentials.getPassword()));
}
}
私は簡単なテストを書いた:
@RunWith(MockitoJUnitRunner.class)
public class LoginUseCaseTest {
private LoginUseCase mLoginUseCase;
@Mock ApiManager mApiManager;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mLoginUseCase = new LoginUseCase(mApiManager);
}
@Test
public void testShouldThrowsError() throws Exception {
TestSubscriber<AuthResponse> testSubscriber = new TestSubscriber<>();
doReturn(Observable.error(new Throwable())).when(mApiManager).login("", "");
mLoginUseCase
.execute(new AuthCredentials("", ""))
.subscribe(testSubscriber);
testSubscriber.assertNoErrors();
}
}
しかし、このテストはいつも通り、この場合モックエラーがどのように観測されるのか分かりません。
EDIT:私はskinnyJに従ってtestShouldThrowsError()を変更しましたが、まだテストは合格していますか?
ここでは遅延が必要な理由は分かりますが、ちょうど+ delayの代わりにhttp://reactivex.io/documentation/operators/timer.html演算子を使用すると短くなります。 – marwinXXII