假设我在python中设置了这样一个类。from somewhere import sendmail
class MyClass:
def __init__(self, **kargs):
self.sendmail = kwargs.get("sendmail", sendmail) #if we can't find it, use imported def
def publish():
#lots of irrelevant code
#and then
self.sendmail(mail_to, mail_from, subject, body, format= 'html')
如您所见,我给了自己一个选项来参数化self.sendmail使用的函数
现在在测试文件中。Class Tester():
kwargs = {"sendmail": MagicMock(mail_from= None, mail_to= None, subject= None, body= None, format= None)}
self.myclass = MyClass(**kwargs)
##later on
def testDefaultEmailHeader():
default_subject = "Hello World"
self.myclass.publish()
self.myclass.sendmail.assert_called() #this is doing just fine
self.myclass.sendmail.assert_called_with(default_subject) #this is having issues
出于某种原因,我收到了错误消息AssertionError: Expected call: mock('Hello World')
Actual Call : mock('defaultmt', 'defaultmf', 'Hello World', 'default_body', format= 'html')
所以基本上,断言期望sendmail只被一个变量调用,而最终被所有5个变量调用。问题是,我不在乎其他4个变量是什么!我只想确定它是用正确的主题调用的。
我试过模拟的占位符,得到了同样的结果self.myclass.sendmail.assert_called_with(ANY, ANY, 'Hello World', ANY, ANY)
AssertionError: Expected call: mock(, , 'Hello World', , )
Actual Call : mock('defaultmt', 'defaultmf', 'Hello World', 'default_body, 'format= 'html')
真的不确定如何继续这个。如果我们只关心其中一个变量而想忽略其余的变量,有人有什么建议吗?