2017-06-01 177 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

您使用驗證,如在示例中 –

相關問題