I've set up a test within Django
that sends an email, in the background, using Django-RQ
.
I call the code that enqueues the send_email
task, then, I get the django-rq
worker, and call .work(), with burst=True
.
In my console, I can see the django-rq
worker picking up the job, and confirming that it's been processed successfully. However, the email never arrives in Django's test mail.outbox
.
Here is the test code:
def test_reset_password(self):
c = Client()
page = reverse_lazy("accounts:password_reset")
response = c.post(page, {'email': 'joe@test.co.uk'})
self.assertRedirects(response, reverse_lazy("accounts:login"), 302, 200)
django_rq.get_worker(settings.RQ_QUEUE).work(burst=True)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Password reset instructions')
And here's the output from the console when the test is run:
Found 2 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.Worker rq:worker:0e599ef7e7e34e30a1b753678777b831: started, version 1.12.0
Subscribing to channel rq:pubsub:0e599ef7e7e34e30a1b753678777b831
*** Listening on default...
Cleaning registries for queue: default
default: send() (214c3a65-c642-49f2-a832-cb40750fad2a)
default: Job OK (214c3a65-c642-49f2-a832-cb40750fad2a)
Result is kept for 500 seconds
Worker rq:worker:0e599ef7e7e34e30a1b753678777b831: done, quitting
Unsubscribing from channel rq:pubsub:0e599ef7e7e34e30a1b753678777b831
F
======================================================================
FAIL: test_reset_password (accounts.tests.AccountsTest.test_reset_password)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/workspace/accounts/tests.py", line 30, in test_reset_password
self.assertEqual(len(mail.outbox), 1)
AssertionError: 0 != 1
----------------------------------------------------------------------
Ran 2 tests in 1.686s
FAILED (failures=1)
Is it possible the email is going to a different instance of the django.core.mail.mailbox
? and if so, is there an effective way to unit test emails, in this context ?