为什么在单元测试中演示者内部的 Observable 中不调用视图方法?
Why does the view methods does not get called inside an Observable which is inside the presenter on a unit test?
我是 Android 单元测试的新手,我们目前正在使用 MVP+RxJava+Dagger 2。我编写的这个测试在单元测试中失败,但在生产代码中有效:
@Override
public void retrieveListOfBillers() {
getMvpView().showLoading();
getCompositeDisposable().add(
getDataManager()
.doServerGetBillersList()
.observeOn(getSchedulerProvider().ui())
.subscribeOn(getSchedulerProvider().io())
.subscribe( response ->{
for (Datum data : response.getData()) {
getMvpView().setUpRecyclerView(enrollmentBillers);
getMvpView().showDefaultViews();
getMvpView().hideLoading();
}, throwable -> {
...
这就是我在测试中的做法:
@Test
public void testGetListOfBillersCallsSetupRecyclerView(){
mPresenter.retrieveListOfBillers();
verify(mView).showLoading();
verify(mView).setUpRecyclerView(anyList());
}
这就是我实例化测试设置的方式:
@Before
public void setUp() {
// Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
// inject the mocks in the test the initMocks method needs to be called.
MockitoAnnotations.initMocks(this);
CompositeDisposable compositeDisposable = new CompositeDisposable();
mTestScheduler = new TestScheduler();
testSchedulerProvider = new TestSchedulerProvider(mTestScheduler);
mPresenter = new CreateBillerContactPresenter<>(
dataManager,
testSchedulerProvider,
compositeDisposable
);
mPresenter.onAttach(mView);
when(dataManager.doServerGetBillersList()).thenReturn(Observable.just(getBillerListResponse));
我相信它与 TestScheduler 有关,但我需要有人真正知道这里的问题是什么,这就是为什么我的测试代码无法调用 setupRecyclerView 以及来自演示者的其他预期视图方法调用的原因?
我找到了答案:
似乎 TestScheduler class 有一个 triggerAction 方法,其中:
"Triggers any actions that have not yet been triggered and that are scheduled to be triggered at or before this Scheduler's present time." -- 来自方法上方的注释。
然后 presenter/datamanager 按预期调用视图方法。
我是 Android 单元测试的新手,我们目前正在使用 MVP+RxJava+Dagger 2。我编写的这个测试在单元测试中失败,但在生产代码中有效:
@Override
public void retrieveListOfBillers() {
getMvpView().showLoading();
getCompositeDisposable().add(
getDataManager()
.doServerGetBillersList()
.observeOn(getSchedulerProvider().ui())
.subscribeOn(getSchedulerProvider().io())
.subscribe( response ->{
for (Datum data : response.getData()) {
getMvpView().setUpRecyclerView(enrollmentBillers);
getMvpView().showDefaultViews();
getMvpView().hideLoading();
}, throwable -> {
...
这就是我在测试中的做法:
@Test
public void testGetListOfBillersCallsSetupRecyclerView(){
mPresenter.retrieveListOfBillers();
verify(mView).showLoading();
verify(mView).setUpRecyclerView(anyList());
}
这就是我实例化测试设置的方式:
@Before
public void setUp() {
// Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
// inject the mocks in the test the initMocks method needs to be called.
MockitoAnnotations.initMocks(this);
CompositeDisposable compositeDisposable = new CompositeDisposable();
mTestScheduler = new TestScheduler();
testSchedulerProvider = new TestSchedulerProvider(mTestScheduler);
mPresenter = new CreateBillerContactPresenter<>(
dataManager,
testSchedulerProvider,
compositeDisposable
);
mPresenter.onAttach(mView);
when(dataManager.doServerGetBillersList()).thenReturn(Observable.just(getBillerListResponse));
我相信它与 TestScheduler 有关,但我需要有人真正知道这里的问题是什么,这就是为什么我的测试代码无法调用 setupRecyclerView 以及来自演示者的其他预期视图方法调用的原因?
我找到了答案: 似乎 TestScheduler class 有一个 triggerAction 方法,其中:
"Triggers any actions that have not yet been triggered and that are scheduled to be triggered at or before this Scheduler's present time." -- 来自方法上方的注释。
然后 presenter/datamanager 按预期调用视图方法。