简体   繁体   中英

Testing if mock was called with other mock

I'm trying to test if a mock object was called with another mock object.

@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

The first two assert s pass, whereas the last one gives a

AssertionError: Expected call: Xero(<MagicMock name='PublicCredentials' id='4377636560'>)
Actual call: Xero(<MagicMock name='PublicCredentials()' id='4377382544'>)

What would be the correct way to verify the XeroMock was called with the CredentialsMock ?

Your code called the CredentialsMock mock object, presumably to create an instance. Note the () in the resulting name:

<MagicMock name='PublicCredentials()' id='4377382544'>
#                                 ^^ called

while you passed in just the mock itself:

<MagicMock name='PublicCredentials' id='4377636560'>
#                                ^ not called

Test for the return_value result:

XeroMock.assert_called_with(CredentialsMock.return_value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM