私は電子メールの件名とメッセージをフォーマットするsend_formatted_email
という関数を書いて、別のモジュールのsend_email
関数を呼び出します。モックがモジュール機能で動作しない
ここで、send_formatted_email
が期待される引数でsend_email
を呼び出していることをテストする必要があります。この目的のために私はpatch
を使ってsend_email
を嘲笑しようとしていますが、嘲笑されていません。
test.py
@patch('app.util.send_email')
def test_send_formatted_email(self, mock_send_email):
mock_send_email.return_value = True
response = send_formatted_email(self.comment, to_email)
mock_send_email.call_args_list
....
views.py
def send_formatted_email(comment, to_email):
...
message = comment.comment
subject = 'Comment posted'
from_email = comment.user.email
...
return send_email(subject, message, to_email, from_email)
util.py
def send_email(subject, message, to, from):
return requests.post(
...
)
私もapp.util.send_email = MagicMock(return_value=True)
を試してみましたが、これはどちらか動作しませんでした。私が間違って何をしているのか?
*定義されている場所ではなく、関数が*使用されている箇所にパッチを当てます。 '@patch( 'app.views.send_email')' – jonrsharpe
[モックの@patchを使って別のPythonモジュールで定義された関数をモックする方法(http://stackoverflow.com/questions/14654009/how-機能別に定義された別のPythonモジュールを使用した模擬パッチ) – jonrsharpe
@jonrsharpeありがとうございました。 –