Mock not working on module function

I wrote a function send_formatted_emailthat formats the subject and email, then calls the function send_emailin a separate module.

Now I have to check what send_formatted_emailcauses send_emailwith the expected arguments. To do this, I try to ridicule send_emailwith the help patch, but he does not scoff.

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(
        ...
    )

I even tried app.util.send_email = MagicMock(return_value=True), but that didn't work either. Any idea what I'm doing wrong?

+4
source share
1 answer

jonrsharpe , .

, ( ).

:

def send_formatted_email(comment, to_email):
    ...
    message = comment.comment
    subject = 'Comment posted'
    from_email = comment.user.email
    ...
    from app.util import send_email
    return send_email(subject, message, to_email, from_email)

.

:

  • .
+1

Source: https://habr.com/ru/post/1651211/


All Articles