gpt4 book ai didi

unit-testing - 在没有 Controller 的情况下测试电子邮件发送

转载 作者:行者123 更新时间:2023-12-04 04:49:15 28 4
gpt4 key购买 nike

我的应用程序发送了很多电子邮件。官方文档告诉how to test emails sending使用 HTTP 客户端分析器。

但我显然没有为我发送的每封电子邮件设置一个 Controller 。

如何测试不是由 Controller 触发的电子邮件? (我的意思是不是来自请求上下文)。


关于我的具体案例的更多细节

我正在使用监听器触发对执行“电子邮件路由”服务的调用
我想在这里做的是触发一些事件并检查发送的内容(电子邮件正文、标题、收件人......)

最佳答案

关注此great post我扩展了 Swift_MemorySpool

class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
{
public function count()
{
return count($this->messages);
}

public function getMessages()
{
return $this->messages;
}
}

将其传递给我的服务使用的邮件程序

class MailServiceTest extends KernelTestCase
{
protected $mailService;
protected $spool;
// ...
protected function setUp()
{
$this->spool = new CountableMemorySpool();
$transport = new \Swift_Transport_SpoolTransport(
new \Swift_Events_SimpleEventDispatcher(),
$this->spool
);
$mailer = new \Swift_Mailer($transport);
$this->mailService->setMailer($mailer);
}

并测试

    // ...
public function testMailSpec()
{
// ... Sending the mail ...
$this->assertCount(1, $this->spool,
'Expected exactly one email to be sent');
$msg = $this->spool->getMessages()[0];
$this->assertArrayHasKey('tartempion@test.com', $msg->getTo(),
'Wrong recipient');
$this->assertContains('Tartempion', $msg->getBody(),
'Expected some string contained in the email body');
}
}

关于unit-testing - 在没有 Controller 的情况下测试电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32903655/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com