2017-02-11 28 views
0

我已經添加期望像這樣的方法,的Android EasyMock的,如何添加一個更多的期待,無需重新設置模擬

expect(locationManager.isLocationSettingsEnabled(anyObject(FragmentActivity.class))).andReturn(isLocationEnabled).anyTimes(); 

replay(locationManager); 

但是,當我加入,重播後,多了一個方法(下面提到的)預期,第一種方法會自動重置。

expect(locationManager.isNotificationSettingsEnabled(anyObject(FragmentActivity.class))).andReturn(isNotificationsEnabled).anyTimes(); 

我想知道如何在不重置的情況下添加一個期望值。

+0

我想你應該使用[谷歌模擬](http://stackoverflow.com/questions/5743236/google-mock-multiple-expectations-on-same-function-with-different-parameters)或[mockito]( http://stackoverflow.com/questions/8088179/using-mockito-with-multiple-calls-to-the-same-method-with-the-same-arguments)。 –

回答

1

Easymock在這個原理上的功能。

  • 當您對方法設置了一些期望值時,通常會僞造/嘲笑該方法的行爲。
  • 現在,當您調用replay(mockObject)時,Easymock會在Test Runner環境中注入這種模擬行爲。

因此,在重播模擬對象之前,您需要對模擬對象執行所有期望。

是這樣的:

EasyMock.expect(mockObject.method1()).andReturn(null); 
EasyMock.expect(mockObject.method2()).andReturn(null); 

EasyMock.replay(mockObject); 

你的問題密切看,我看你是嘲諷與兩個不同的回報條款的單一方法,你 可以做這樣的事情:

EasyMock.expect(mockObject.method1()).andReturn(new Integer(1)).once(); 
EasyMock.expect(mockObject.method1()).andReturn(new Integer(2)).once(); 

EasyMock.replay(mockObject); 

通過這Easymock將返回1作爲第一次調用方法時的輸出和2時調用方法第二次。

希望這有助於!

祝你好運!

+1

作爲一個方面說明,'once()'不需要,因爲它是默認值。你也可以管道調用'和返回(1)和返回(2)'。 – Henri

+0

@亨利謝謝你指出 – Vihar

相關問題