我想测试我的 Presenter
public class MainPresenter extends MvpBasePresenter<MainView> {
private Repository repository;
private final CompositeDisposable disposables = new CompositeDisposable();
public void setRepository(Repository repository) {
this.repository = repository;
}
public void loadFromRepository() {
getView().showLoading(false);
disposables.add(repository.getCountries()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribeWith(new DisposableObserver<List<Country>>() {
@Override
public void onNext(List<Country> countries) {
if (isViewAttached()) {
getView().setData(countries);
getView().showContent();
}
}
@Override
public void onError(Throwable e) {
if (isViewAttached()) {
getView().showError(e, false);
}
}
@Override
public void onComplete() {
}
}));
}
public void loadFromRemoteDatastore() {
disposables.add(new RemoteDataStore().getCountries()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribeWith(new DisposableObserver<List<Country>>() {
@Override
public void onNext(List<Country> countries) {
if (isViewAttached()) {
getView().setData(countries);
getView().showContent();
}
}
@Override
public void onError(Throwable e) {
if (isViewAttached()) {
getView().showError(e, false);
}
}
@Override
public void onComplete() {
}
}));
}
@Override
public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
if (!retainInstance) {
disposables.clear();
}
}
}
但是,我有很多疑问,最好的测试方法是什么
1)我写这4个测试场景可以吗
shouldShowContentWhenLoadFromRepository()
shouldShowErrorWhenLoadFromRepository()
shouldShowContentWhenLoadFromRemoteDatastore()
shouldShowErrorWhenLoadFromRemoteDatastore()
2) 我是否应该为 detachView(boolean retainInstance) 编写测试并清除一次性用品
3) 在我的情况下,哪种机制最适合测试 RxJava?