简体   繁体   English

如何模拟一个方法,看看它是否被调用?

[英]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. 我应该如何对这种情况进行单元测试,其中is_send_emails参数为false,我必须断言sendEmails()未被调用。

I thought I should mock self.sendEmails() to see if it was called at all. 我以为我应该模拟self.sendEmails()来查看它是否被调用。

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 ? 我应该使用assert_called_with吗?

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

self.assertFalse(sendEmails_mock.called)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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