第一次使用MagicMock,我想我已经迷惑了自己。
测试一个django项目,所以在一个名为services.py
的文件中,我有这些重要的元素(非常简化,当然还有很多位)::
from django.template.loader import get_template
class Email:
def send_success_attempt_email(self):
template = get_template('emails/foo.html')
我想测试一下,当send_success_attempt_email
被调用时,get_template
会被正确的参数调用。所以我用补丁写了一个测试:
@patch('django.template.loader.get_template')
def test_email_template_should_be_used(self, get_template):
email = Email()
email.send_success_attempt_email()
print(get_template.call_count)
get_template.assert_called_with('emails/foo.html')
为0
打印call_count
,并吐出
AssertionError nose.proxy.AssertionError:
Expected call: get_template('emails/foo.html')
Not called
我已经看到一个常见的陷阱是修补错误的实例,但我在补丁上尝试了很多变体(例如,@patch('services.get_template')
)但是这会改变错误({{1} }),它并没有减轻它。
我知道我必须有一个根本的误解。它是什么?