Laravel单元测试电子邮件 [英] Laravel unit testing emails
问题描述
我的系统发送了一些重要的电子邮件.进行单元测试的最佳方法是什么?
My system sends a couple of important emails. What is the best way to unit test that?
我看到您可以将其置于伪装模式,并且它会记录在日志中.有什么要检查的吗?
I see you can put it in pretend mode and it goes in the log. Is there something to check that?
推荐答案
有两个选项.
选项1-模拟邮件外观以测试正在发送的邮件.这样的事情会起作用:
Option 1 - Mock the mail facade to test the mail is being sent. Something like this would work:
$mock = Mockery::mock('Swift_Mailer');
$this->app['mailer']->setSwiftMailer($mock);
$mock->shouldReceive('send')->once()
->andReturnUsing(function($msg) {
$this->assertEquals('My subject', $msg->getSubject());
$this->assertEquals('foo@bar.com', $msg->getTo());
$this->assertContains('Some string', $msg->getBody());
});
选项2更容易-使用 MailCatcher.me 测试实际的SMTP.基本上,您可以发送SMTP电子邮件,并测试" 实际发送的电子邮件. Laracasts在这里提供了关于如何将其用作Laravel测试一部分的很好的教训
Option 2 is much easier - it is to test the actual SMTP using MailCatcher.me. Basically you can send SMTP emails, and 'test' the email that is actually sent. Laracasts has a great lesson on how to use it as part of your Laravel testing here.
这篇关于Laravel单元测试电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!