用DermobleObserver测试主持人



我正在开发一个Android应用程序,试图遵循清洁体系结构/MVP指南。

我目前正在为我的演示者进行对单元测试,但是我被访问互动器/用户酶的呼吁所困扰,该呼叫将DispoSobleObserver作为参数。

我想测试的是,当交互式调用Onnext或OnError时,称为正确的行为(hide/show loading指示器...(。

我不知道如何使用受保护的方法在调用execute((方法时构建可观察到的行为。

以下是代码的某些部分:

主持人

@ConfigPersistent
public class ContentPresenter extends BasePresenter<ContentContract.View> implements ContentContract.Presenter {
    @Inject
    GetContent mGetContentUseCase;
    @Inject
    ContentViewModelMapper mContentViewModelMapper;
    @Inject
    public ContentPresenter() {
    }
    @Override
    public void fetchContent(long contentId) {
        getMvpView().showProgress();
        mGetContentUseCase.execute(contentId, new ContentObserver());
    }
    private final class ContentObserver extends DisposableObserver<Content> {
        @Override
        public void onNext(Content content) {
            getMvpView().hideProgress();
            getMvpView().showContentInfo(mContentViewModelMapper.map2(content));
        }
        @Override
        public void onError(Throwable e) {
            getMvpView().hideProgress();
            Timber.e(e.getMessage());
        }
        @Override
        public void onComplete() {
            getMvpView().hideProgress();
        }
    }
}

互动/用户酶

public class GetContent extends UseCaseObservableWithParameter<Long, Content, Repository> {
    @Inject
    public GetContent(Repository repository,
                         @Named("Thread") Scheduler threadScheduler,
                         @Named("PostExecution") Scheduler postExecutionScheduler) {
        super(repository, threadScheduler, postExecutionScheduler);
    }
    @Override
    protected Observable<Content> buildObservable(Long id) {
        return repository.getContentById(id);
    }
}

baseusecase

public abstract class UseCaseObservableWithParameter<REQUEST_DATA, RESPONSE_DATA, REPOSITORY> extends UseCase<Observable<RESPONSE_DATA>, REQUEST_DATA, REPOSITORY> {
    public UseCaseObservableWithParameter(REPOSITORY repository, Scheduler threadScheduler, Scheduler postExecutionScheduler) {
        super(repository, threadScheduler, postExecutionScheduler);
    }
    protected abstract Observable<RESPONSE_DATA> buildObservable(REQUEST_DATA requestData);
    public void execute(REQUEST_DATA requestData, DisposableObserver<RESPONSE_DATA> useCaseSubscriber) {
        this.disposable.clear();
        this.disposable.add(
                this.buildObservable(requestData)
                        .subscribeOn(threadScheduler)
                        .observeOn(postExecutionScheduler)
                        .subscribeWith(useCaseSubscriber)
        );
    }
}

睡个好觉后,这就是我想到的:

@Test
@SuppressWarnings("unchecked")
public void testShowContents() {
    doAnswer((i) -> {
        // Do stuff with i.getArguments() here
        DisposableObserver<Content> d = i.getArgument(1);
        Observable.just(mock(Content.class)).subscribeWith(d);
        return null;
    })
            .when(mGetContentUseCase)
            .execute(eq(AppTestData.TEST_LONG_ID_1), any(DisposableObserver.class));
    contentPresenter.fetchContent(AppTestData.TEST_LONG_ID_1);
    Mockito.verify(view, Mockito.times(1)).showContentInfo(Mockito.any());
    InOrder orderVerifier = Mockito.inOrder(view);
    orderVerifier.verify(view).showProgress();
    orderVerifier.verify(view).hideProgress();
    orderVerifier.verify(view).showContentInfo(any());
}
@Test
@SuppressWarnings("unchecked")
public void testShowContentsError() {
    doAnswer((i) -> {
        // Do stuff with i.getArguments() here
        DisposableObserver<Content> d = i.getArgument(1);
        Observable.<Content>error(new Throwable()).subscribeWith(d);
        return null;
    })
            .when(mGetContentUseCase)
            .execute(eq(AppTestData.TEST_LONG_ID_1), any(DisposableObserver.class));
    contentPresenter.fetchContent(AppTestData.TEST_LONG_ID_1);
    Mockito.verify(view, Mockito.times(1)).showErrorMessage(Mockito.any());
    InOrder orderVerifier = Mockito.inOrder(view);
    orderVerifier.verify(view).showProgress();
    orderVerifier.verify(view).hideProgress();
    orderVerifier.verify(view).showErrorMessage(any());
}

相关内容

  • 没有找到相关文章

最新更新