Python补丁无效,未记录任何呼叫

时间:2016-04-14 02:47:44

标签: python django unit-testing python-3.x magicmock

第一次使用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} }),它并没有减轻它。

我知道我必须有一个根本的误解。它是什么?

0 个答案:

没有答案