Houman
Houman

Reputation: 66320

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?

Upvotes: 1

Views: 78

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121834

To test that your mock was not called, just test the called attribute is False:

self.assertFalse(sendEmails_mock.called)

Upvotes: 3

Related Questions