1

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

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

我做了一些研究,并在 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'

根据我的发现,这里的问题是

Django 服务器在与 lettuce 脚本不同的进程中运行,这会使发件箱无法访问。

我做了一些更多的研究,并在这里找到了一个可能的解决方案。

男生是这样说的:

# 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等价物是什么。我在steps.py文件中尝试过,但没有用。

有谁知道如何解决这一问题?

4

1 回答 1

2

经过更多研究后,我设法在这里找到了我的问题的答案。

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

EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
于 2015-07-07T16:00:21.907 回答