0
我想測試模擬對象是否被另一個模擬對象調用。測試模擬是否被其他模擬調用
@patch(__name__ + '.xero_helper.PublicCredentials')
@patch(__name__ + '.xero_helper.Xero')
def testGetValidPublicXeroInstance(self, XeroMock, CredentialsMock):
xero_helper.get_xero_instance('abc') # Do relevant stuff
CredentialsMock.assert_called_with(**org.oauth_credentials) # OK
XeroMock.assert_called_once() # OK
XeroMock.assert_called_with(CredentialsMock) # Not OK
前兩個assert
的妙傳,而最後一個給人以
AssertionError: Expected call: Xero(<MagicMock name='PublicCredentials' id='4377636560'>)
Actual call: Xero(<MagicMock name='PublicCredentials()' id='4377382544'>)
什麼是驗證XeroMock
被稱爲與CredentialsMock
正確的方法是什麼?
在未來,請提供代碼也在測試中;我可以從斷言錯誤中找出問題所在,但實際代碼(或者替代它,產生相同的輸出)會更容易。這被稱爲[MCVE]。 –
另一種選擇可能是重構代碼,所以除了'@ patch'之外,還有其他替代方法可以注入模擬。一個受歡迎的可能是依賴注入。也許'get_xero_instance'可以接受'** kwargs'並允許調用者提供'CredentialsMock'實例? – dm03514