初めて 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')
が、エラー ( nose.proxy.EncodeError: Can't pickle <class 'unittest.mock.MagicMock'>: it's not the same object as unittest.mock.MagicMock
) は変更されますが、軽減されません。
私は根本的な誤解をしているに違いないことを知っています。それは何ですか?