也许我不明白发件箱是如何工作的,但从文档中我了解到它只是在测试期间捕获所有外发邮件。
我使用新应用程序创建了一个新项目并添加了以下代码。
from django.test import TestCase
from django.core.mail import send_mail, outbox
class SimpleTest(TestCase):
def test_basic_addition(self):
send_mail('Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False)
self.assertEqual( len( outbox ), 1 )
当我运行 python manage.py test app_name 时,它给出了一个断言错误 0 != 1。我做错了什么吗?
更新
那么这很奇怪,如果我导入 django.core.mail 并使用 mail.outbox 它确实有效。
尝试比较 outbox 和 mail.outbox 的直接导入,它们都给出了不同的结果
from django.core import mail
from django.core.mail import send_mail, outbox
...
self.assertEqual(outbox, mail.outbox)
返回:
- []
+ [<django.core.mail.message.EmailMessage object at 0x1e1fd90>]
也许我已经工作了很长时间并且错过了一些非常明显的东西?
最佳答案
也许我应该真正阅读文档。
The outbox attribute is a special attribute that is created only when the locmem e-mail backend is used. It doesn't normally exist as part of the django.core.mail module and you can't import it directly.
https://docs.djangoproject.com/en/dev/topics/testing/tools/#email-services
关于Django 1.3 : Outbox empty during tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5424498/