Using this answer as a model, I am testing the following method that instantiates a class (EmailMultiAlternatives
):
admintools/emailer.py
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
def send_email(template_path, context, subject_line, from_email, to=[], cc=[], bcc=DEFAULT_BCC_EMAILS):
msg_html = render_to_string(template_path, context)
msg_plain = strip_tags(msg_html)
email = EmailMultiAlternatives(subject_line, msg_plain, from_email, to, cc=cc, bcc=bcc)
email.attach_alternative(msg_html, "text/html")
email.send()
using test.py:
from unittest.mock import patch
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.test import TestCase
from admintools.emailer import send_email
from admintools.settings import DEFAULT_BCC_EMAILS
class EmailerTestCase(TestCase):
@patch('django.core.mail.EmailMultiAlternatives')
def test_send_email(self, mockEmailMultiAlternatives):
context = {}
template_path = "emails/closings/seller_pre_close_update_prompt.html"
msg_plain = render_to_string(template_path, context)
to = ['']
cc = ['']
send_email(template_path, {}, 'subject', "from", to, cc=cc)
mockEmailMultiAlternatives.assert_called()
I receive AssertionError: Expected 'EmailMultiAlternatives' to have been called.
even though email
is created successfully during the running of the test (verified by a print(email)
immediately after the instantiation returning <django.core.mail.message.EmailMultiAlternatives object at 0x7fbf4bb4b590>
).
What might be causing the assertion to fail even though EmailMultiAlternatives
is being instantiated?
Aucun commentaire:
Enregistrer un commentaire