2017-06-01 184 views
3

我们如何模拟应用上下文?我有一个主持人,我打算为其写一个测试。它收到的参数是vie w和Context。我如何创建一个模拟上下文的工作?如何模拟应用上下文

public TutorProfilePresenter(TutorProfileScreenView view, Context context){ 
    this.view = view; 
    this.context = context 
} 

public void setPrice(float price,int selectedTopics){ 
     int topicsPrice = 0; 
     if(selectedTopics>2) 
     { 
     topicsPrice = (int) ((price/5.0)*(selectedTopics-2)); 
     } 


     view.setBasePrice(price,topicsPrice,selectedTopics, 
         price+topicsPrice); 
} 
+0

添加您要测试的impl并让我们看到 –

+0

我已更新我的问题。请检查它 –

+0

我只是想编写一个示例片段来测试是否调用演示者在视图中(这是一个接口)的方法来更新活动内容。 –

回答

4

为基地我会用注释的Mockito(我假设你想嘲笑观点也):

public class TutorProfilePresenter{ 

    @InjectMocks 
    private TutorProfilePresenter presenter; 

    @Mock 
    private TutorProfileScreenView viewMock; 
    @Mock 
    private Context contextMock; 

    @Before 
    public void init(){ 
     MockitoAnnotations.initMocks(this); 
    } 

    @Test 
    public void test() throws Exception{ 
     // configure mocks 
     when(contextMock.someMethod()).thenReturn(someValue); 

     // call method on presenter 

     // verify 
     verify(viewMock).setBasePrice(someNumber...) 
    } 

} 

这沃尔德注入准备嘲笑配置到你的类测试。

+0

我是gad,它工作。谢谢 –

+0

我们如何确保通过演示者的观点来调用活动?我如何编写测试来检查它? –

+0

您使用验证,如在示例中 –

相关问题