简体   繁体   English

如何在MVP中对演示者进行单元测试

[英]How to unit-test a presenter in a MVP

I have a model view presenter triad. 我有一个模型视图演示者三合一。 I'd like to know what's the usual way to test the presenter. 我想知道测试演示者的常用方法是什么。

The first thing that came to my mind was to instantiate the presenter and instantiate a real view and then assert that the view would accomplish the expected behavior. 我想到的第一件事是实例化演示者并实例化一个真实的视图,然后断言视图将完成预期的行为。

public void itShouldSayHello() {
    View view = new View();
    Presenter presenter = new Presenter(view);
    presenter.userSaid("hello");
    assertTrue(view.getGreeting().equals("hello"));
}

Then I thought that the view was not under test and so I created a fake view. 然后我认为视图没有经过测试,因此我创建了一个假视图。

private String greeting;

public void itShouldSayHello() {
    View view = new FakeView();
    Presenter presenter = new Presenter(view);
    presenter.userSaid("hello");
    assertTrue(greeting.equals("hello"));
}
private class FakeView implements View {
    @Override
    public void displayGreeting(String saluto) {
        greeting = saluto;
    }
}

Then I thought that the interface of the view could change. 然后我认为视图的界面可能会改变。 This would've made the code harder to maintain. 这将使代码难以维护。 So I wrote the test and asserted that something was to be presented to the view. 所以我编写了测试并断言要将某些内容呈现给视图。 This way even if the interface changed I would have to change one line of code in the tests. 这样即使接口改变了,我也不得不在测试中改变一行代码。

public void itShouldSayHello() {
    View view = mock(View.class);
    Presenter presenter = new Presenter(view);
    presenter.userSaid("hello");
    verify(view).displayGreeting("hello");
}

So basically what I test now is that I expect the presenter to gather and process some information and finally pass it to the view, then I verify that the passed values are correct. 所以我现在测试的基本上是我希望演示者收集并处理一些信息并最终将其传递给视图,然后我验证传递的值是否正确。

So I guess I'm not using a fake now, I'm using a mock and then I verify if the mock receives the correct values. 所以我想我现在不使用假的,我正在使用模拟然后我验证模拟是否收到正确的值。

Another problem I have is with the model. 我遇到的另一个问题是模型。 But I think this is insurmountable. 但我认为这是不可逾越的。 What I have to do to see if the presenter behaves correctly is to create a big fat fixture. 我要做的就是要确定演示者是否正常运行是为了创建一个大的脂肪夹具。 Then a pass all the various combinations and see if the presenter behaves correctly. 然后传递所有各种组合,看看演示者的行为是否正确。

How do you test your presenter? 你如何测试你的演示者?

You leverage the separation of view/presenter to test the presenter. 您可以利用视图/演示者的分离来测试演示者。 If you've implemented MVP fully, your View will implement an interface and your Presenter will use that interface to return data to the View. 如果您已完全实现MVP,您的View将实现一个接口,您的Presenter将使用该接口将数据返回到View。 If you are testing your MVP application you will want not just your Presenter to function correctly, but also the interface the View uses to communicate with the Presenter. 如果您正在测试MVP应用程序,您不仅需要Presenter正常运行,还需要View用于与Presenter通信的界面。

So your test class should implement the View's interface, invoke methods of the Presenter, and store responses from the Presenter from the local methods that override the interface. 因此,您的测试类应实现View的接口,调用Presenter的方法,并从覆盖接口的本地方法存储Presenter的响应。 If your Presenter communicates with the business layer synchronously, this is easier: 如果Presenter同步与业务层通信,则更容易:

  1. Define a test class that implements the View interface 定义实现View接口的测试类
  2. Implement the interface in the test class so data prepared by the presenter is stored in the test object 在测试类中实现接口,以便由演示者准备的数据存储在测试对象中
  3. Create test methods in the test class that invoke the presenter, get the response from the object, and perform the appropriate comparisons. 在测试类中创建调试方法,调用演示者,从对象获取响应,并执行适当的比较。

If your Presenter communicates asynchronously you will have to do something like wait in the test method and notify from the interface methods. 如果您的Presenter异步通信,则必须执行类似等待测试方法并通过接口方法进行通知的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM