Hi folks,
I'm trying to use
mock.patch to test that a particular post-save signal kicks off an email. My current approach to this is...
from django.test import TestCase
from mock import patch
class RegisterTestCase(TestCase):
def test_mail_is_sent(self):
with patch('django.core.mail.send_mail') as mocked_send_mail:
from register.models import Subscriber #This test still fails if I have the import outside the mock context
subscriber = Subscriber.objects.create(
)
self.assertTrue(mocked_send_mail.called)
self.assertFalse(subscriber.active)
def test_patching_mail_works(self):
with patch('django.core.mail.send_mail') as mocked_send_mail:
from django.core.mail import send_mail #This test actually fails too if I have the import outside the mock context
send_mail(subject="foo", message="bar", from_email="Andrew M. Farrell <amfa...@mit.edu>", self.assertTrue(mocked_send_mail.called)
The first test fails while the second test passes.
As I understand from reading the mock.patch documentation, patch('django.core.mail.send_mail') should cause all references to that object to instead referr to an instance of MagicMock. However, I can verify by inserting a pdb.set_trace() call into the signal handler that send_mail is in fact called during the test and that send_mail.func_code is <code object send_mail at 0x103492330, file "//anaconda/envs/taxbrain/lib/python2.7/site-packages/django/core/mail/__init__.py", line 41>.
Are there other things that could cause a function to not be mocked properly?
Is my interpretation of the documentation just incorrect?
thank you,
Andrew