我在构建我的应用程序时正在实现MVP架构,我从一些网站上获得了MVP教程。我想测试项目中的每个方法。当我测试演示者时,发生错误。它表明预期参数和实际参数是不同的。
我发现发生此错误是因为我在实际方法中使用了this
关键字。我使用this
是因为我的演示者实现了一些接口(OnFinishedListener
)。
这是我的演示者代码和演示者测试。
MainPresenter
public class MainPresenter implements MainContract.Presenter, MainContract.Model.OnFinishedListener {
private MainContract.View mainView;
private MainContract.Model mainModel;
private RetrofitServices retrofitServices;
public MainPresenter(MainContract.View mainView, RetrofitServices retrofitServices, MainModel mainModel) {
this.mainView = mainView;
this.retrofitServices = retrofitServices;
this.mainModel = mainModel;
}
@Override
public void onFinished(Inquiry inquiry) {
if(mainView != null){
mainView.hideProgress();
mainView.setDataToViews(inquiry);
}
}
@Override
public void onFailure(Throwable t) {
if(mainView != null){
mainView.hideProgress();
mainView.onResponseFailure(t);
}
}
@Override
public void onDestroy() {
mainView = null;
}
@Override
public void requestInquiryData(Inquiry inquiry) {
if(mainView != null){
mainView.showProgress();
}
mainModel.getInquiry(this, inquiry, retrofitServices);
}
}
MainPresenterTest
public class MainPresenterTest {
private MainPresenter mainPresenter;
@Mock
RetrofitServices retrofitServices;
@Mock
MainContract.View view;
@Mock
Inquiry inquiry;
@Mock
Throwable throwable;
@Mock
MainContract.Model.OnFinishedListener listener;
@Mock
MainModel mainModel;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void onFinished_hideProgress_setDataToViews() {
mainPresenter = new MainPresenter(view, retrofitServices, mainModel);
mainPresenter.onFinished(inquiry);
verify(view).hideProgress();
verify(view).setDataToViews(inquiry);
}
@Test
public void onFinished_doNothing_whenViewIsNull() {
mainPresenter = new MainPresenter(null, retrofitServices, mainModel);
mainPresenter.onFinished(inquiry);
verifyZeroInteractions(view);
}
@Test
public void onFailure_hideProgress_responseFailure() {
mainPresenter = new MainPresenter(view, retrofitServices, mainModel);
mainPresenter.onFailure(throwable);
verify(view).hideProgress();
verify(view).onResponseFailure(throwable);
}
@Test
public void onFailure_doNothing_whenViewIsNull() {
mainPresenter = new MainPresenter(null, retrofitServices, mainModel);
mainPresenter.onFailure(throwable);
verifyZeroInteractions(view);
}
@Test
public void requestInquiryData_showProgress_getInquiryCalled() {
mainPresenter = new MainPresenter(view, retrofitServices, mainModel);
mainPresenter.requestInquiryData(inquiry);
verify(view).showProgress();
verify(mainModel).getInquiry(listener, inquiry, retrofitServices);
}
我想在演示者中测试requestInquiryData
方法(使用requestInquiryData_showProgress_getInquiryCalled
),但是发生了该错误。使用关键字this
的方法如何测试方法?
谢谢您的回答。
答案 0 :(得分:0)
我通过用verify(mainModel).getInquiry(listener, inquiry, retrofitServices);
替换verify(mainModel).getInquiry(mainPresenter, inquiry, retrofitServices);
上的监听器来解决此问题,因为this
上的mainModel.getInquiry(this, inquiry, retrofitServices);
引用了MainPresenter.java
类而不是MainContract.Model.OnFinishedListener
>
我不知道这是否是在Android开发中测试MVP架构的最佳实践,请随时纠正我。谢谢