用生菜测试时无法访问发件箱

Can't access outbox while testing with lettuce

我正在使用 Lettuce 来测试我的一个应用程序。

我正在测试一个模块来检查我是否可以发送电子邮件。

我做了一些研究,并在 Django 文档中找到了一种简单的测试方法。

from django.core import mail
from django.test import TestCase

class EmailTest(TestCase):
    def test_send_email(self):
        # Send message.
        mail.send_mail('Subject here', 'Here is the message.',
            'from@example.com', ['to@example.com'],
            fail_silently=False)

        # Test that one message has been sent.
        self.assertEqual(len(mail.outbox), 1)

        # Verify that the subject of the first message is correct.
        self.assertEqual(mail.outbox[0].subject, 'Subject here')

问题是我一直收到错误 AttributeError: 'module' object has no attribute 'outbox'

据我所知 found,这里的问题是

The Django server runs in a different process from the lettuce scripts, which would make the outbox inaccessible.

我做了更多研究并找到了可能的解决方案 here

这家伙是这样说的:

# in terrain.py
from lettuce import before, after, world
from django.conf import settings

@before.handle_request
    def override_mail_settings(httpd, server):
        settings.EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'

但是我不知道我的terrain.py equivalent.I在steps.py文件中试过什么,但是没有用。

有人知道如何解决这个问题吗?

经过更多研究,我设法找到了 here 问题的答案。

我唯一要做的就是编辑 settings.py 并添加这一行:

EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'