[英]Python unittest test passed in class method is called with
I have a method that takes two inputs a emailer class and a dict of data. 我有一个方法需要两个输入,一个电子邮件类和一个数据字典。
def send_email(data, email_client):
**** various data checks and formatting *****
response_code = email_client.create_email(recipient=receipient
sender=sender
etc...)
I am trying to write a unit test that will assert email_client.create_email was called with the correct values based on the input data. 我正在尝试编写一个单元测试,将断言基于输入数据使用正确的值调用了email_client.create_email。
In my test file I have 在我的测试文件中
from emailer.email import send_email
class TestEmails(unittest.TestCase):
def test_send_email(self):
email.send_email(get_transactional_email_data, MagicMock())
I normally test what a method is called with by something similar to: 我通常会测试类似于以下内容的方法所调用的内容:
mock.assert_called_with(recipient=receipient
sender=sender
etc..)
However, since this time I'm testing what a passed in class is being called with (and a MagicMock) I don't know how it should be done. 但是,由于这一次我正在测试用(和MagicMock)调用传入的类,所以我不知道该怎么做。
I don't think you need MagicMock. 我认为您不需要MagicMock。 Just create the mocks upfront
只是预先创建模拟
from emailer.email import send_email
class TestEmails(unittest.TestCase):
def test_send_email(self):
myclient = Mock()
mycreate = Mock()
myclient.create_email = mycreate
email.send_email(get_transactional_email_data, myclient)
self.assertTrue(
mycreate.called_with(sender='...')
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.