I am trying to mock the sendEmails()
method and would like to test if the second parameter is called with "test@test.com" email address.
@mock.patch('apps.dbank.management.commands.optin_invites.OptinBase.sendEmails')
def test_x_send_emails(self, send_emails_mock):
oi = OptinInvitesX()
oi.compute(True, "test@test.com")
self.assertTrue(send_emails_mock.assert_called_with(???, test_email_address="test@test.com"))
I could utilise assert_called_with
but I don't care about the first parameter for this test case. Is there a way to say accept anything for first parameter?
You are describing the basic usage of mock.ANY
:
Sometimes you may need to make assertions about some of the arguments in a call to mock, but either not care about some of the arguments or want to pull them individually out of
call_args
and make more complex assertions on them.To ignore certain arguments you can pass in objects that compare equal to everything. Calls to
assert_called_with()
andassert_called_once_with()
will then succeed no matter what was passed in.
So, in your case, you could use:
# only asserting on 'test_email_address' argument:
send_emails_mock.assert_called_with(mock.ANY, test_email_address="test@test.com")
Note that you don't really want to use self.assertTrue
on that line. The mock method is its own assertion.