0
關於GWT testing的部分描述瞭如何驗證Presenter
在Display
對象中的輸出,但並未解釋如何以相反的方式進行操作。換句話說,當用戶點擊Display
中的按鈕時,我想檢查Presenter
是否會向RPC Service
發出正確的請求。如何模擬View/Display中的點擊事件以進行測試?
如何模擬Display
中的按鈕點擊?發射GWT事件並不簡單,因爲它們具有受保護的構造函數。有沒有辦法簡單地做到這一點,而不需要子類ClickEvent
類?
@Before
protected void setUp() {
mockRpcService = mock(NegotiationServiceAsync.class);
eventBus = new HandlerManager(null);
mockDisplay = mock(NegotiationPresenter.Display.class);
negotiationPresenter = new NegotiationPresenter(mockRpcService,
eventBus, mockDisplay);
}
@Test
private void testSth() {
when(mockDisplay.getSuppliersEmails()).thenReturn("[email protected]");
when(mockDisplay.getTaskDescription()).thenReturn("This is the task to do");
// This does not work
mockDisplay.getSubmitButton().fireEvent(new ClickEvent());
verify(mockRpcService).startTask(any(NegotiationRequest.class), any(AsyncCallback.class));
}
+1您的答案可以解決問題。你能推薦一個教程來正確測試GWT應用程序嗎?谷歌的[教程](https://developers.google.com/web-toolkit/articles/mvp-architecture)在這裏頗具誤導性。首先,處理方法像'onSubmit()'在Presenter中是私有的,因此阻礙了測試。其次,Presenter知道屬於GWT包的'ClickEvent',而它應該接收'SubmitEvent'來讓Presenter測試。 – dzieciou 2013-02-24 06:32:15
不要將事件傳遞給演示者。在視圖中你應該有這樣的東西:'submitButton(new ClickEvent()){presenter.submit()}'。在演示者中,您可以調用視圖來獲取值:'public submit(){view.getWhatever()...}'。因此,您可以看到視圖使用演示者在事件觸發時調用方法,並且演示者從視圖中獲取和設置值。測試方法的一種常見做法是使其受到保護,併爲測試類使用名稱空間。喲閱讀[this](https://developers.google.com/web-toolkit/articles/testing_methodologies_using_gwt)? – 2013-02-24 11:56:08