简体   繁体   中英

How to mock a method and see if it was called at all?

I have a method that ends like this:

def compute(self, is_send_emails, test_email_address):
    ...
    if is_send_emails:
          self.sendEmails(uniq_email_pids=uniq_email_pids,
                                    test_email_address=test_email_address)
    else:
          logging.debug("send_emails = False - No emails were sent out.")

How should I unit test this case, where is_send_emails parameter is false and I have to assert that sendEmails() was not called.

I thought I should mock self.sendEmails() to see if it was called at all.

def test_x(self):
    with mock.patch('apps.dbank.x.sendEmails') as sendEmails_mock:

But now I am stuck, how to check that. This site explains the different asserts I could use, but none of them seems appropriate. Should I use assert_called_with ?

要测试调用mock,只需测试被called属性False

self.assertFalse(sendEmails_mock.called)

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