I am trying to test a function I created to send a welcome e-mail to a user. But, to do this, I have to mock the function that actually sends it (inside the welcome e-mail function). I have the following folder structure:
app/
__init__.py
mail.py
tests/
__init__.py
conftest.py
test_mail.py
This is the code I have in mail.py
import flask_mail
mail = flask_mail.Mail()
def send_mail(subject, sender, recipients, text_body, html_body):
msg = flask_mail.Message(subject, sender=sender, recipients=recipients, body=text_body, html=html_body)
mail.send(msg)
def send_sign_up_mail(user):
subject = "Test subject"
sender = ("Test sender", "testsender@gmail.com")
recipients = [user.email]
text_body = "text body"
html_body = f"Test html body"
send_mail(subject, sender, recipients, text_body, html_body)
And this is code of test_mail.py, the test I am trying to create:
from unittest import mock
from app.mail import send_sign_up_mail
@mock.patch('app.mail.send_mail')
def test_send_sign_up_mail(mock_send_mail, user):
send_sign_up_mail(user)
assert mock_send_mail.call_count == 1
The argument user is a fixture I created, and it is working, so, don't need to worry about that.
Using a pdb debugger, I checked that the send_mail function is not being mocked inside of send_sign_up_mail.