Php Laravel通知邮件无法在测试中工作
我正在使用通知在触发通知时向所有用户发送邮件 这在手动“浏览网站”使用(向ContactController提交帖子)中效果很好,但在我的测试中,mail facade声称没有发送邮件 控制器:Php Laravel通知邮件无法在测试中工作,php,laravel,testing,Php,Laravel,Testing,我正在使用通知在触发通知时向所有用户发送邮件 这在手动“浏览网站”使用(向ContactController提交帖子)中效果很好,但在我的测试中,mail facade声称没有发送邮件 控制器: 发送通知电子邮件的驱动程序似乎没有使用邮件外观,这意味着邮件::假不会影响它。相反,它直接调用Mailable上的send方法,该方法依次调用Mailer(邮件驱动程序)上的send 您可以将Mailable实例替换为的实例(这是Mail::fake使用的),但它看起来似乎不适合mailfeak是数组的
发送通知电子邮件的驱动程序似乎没有使用邮件
外观,这意味着邮件::假
不会影响它。相反,它直接调用Mailable
上的send
方法,该方法依次调用Mailer
(邮件驱动程序)上的send
您可以将Mailable
实例替换为的实例(这是Mail::fake
使用的),但它看起来似乎不适合mailfeak
是数组的情况(这是MailChannel
传递给Mailable
的内容)
幸运的是,Laravel源代码包含了一个示例,说明了他们如何测试发送邮件的通知。他们模拟Mailer
和Markdown
实例,并检查传递的参数。你可以做类似的事情,比如:
use Mockery as m;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Markdown;
use Illuminate\Mail\Message;
class ContactFormTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();
m::close();
}
protected function setUp(): void
{
parent::setUp();
$this->mailer = m::mock(Mailer::class);
$this->markdown = m::mock(Markdown::class);
$this->instance(Mailer::class, $this->mailer);
$this->instance(Mailer::class, $this->markdown);
}
public function a_mail_is_send_when_the_contact_form_is_used()
{
$this->withExceptionHandling();
$user = factory(User::class)->create();
$this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
$this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
$data = [
'name' => 'John Doe',
'email' => 'email@email.com',
'message' => 'This is a test message'
];
$notification = new ContactRequestNotification($data);
$this->mailer->shouldReceive('send')->once()->with(
['html' => 'htmlContent', 'text' => 'textContent'],
array_merge($notification->toMail($user)->toArray(), [
'__laravel_notification' => get_class($notification),
'__laravel_notification_queued' => false,
]),
m::on(function ($closure) {
$message = m::mock(Message::class);
$message->shouldReceive('to')->once()->with([$user->email]);
$closure($message);
return true;
})
);
$response = $this->post('/contact', $data);
$response->assertStatus(200);
}
}
就个人而言,我现在更愿意在ContactRequestNotification
类上对toMail
方法进行单元测试,因为我认为上面的方法不是很好
use Mockery as m;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Markdown;
use Illuminate\Mail\Message;
class ContactFormTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();
m::close();
}
protected function setUp(): void
{
parent::setUp();
$this->mailer = m::mock(Mailer::class);
$this->markdown = m::mock(Markdown::class);
$this->instance(Mailer::class, $this->mailer);
$this->instance(Mailer::class, $this->markdown);
}
public function a_mail_is_send_when_the_contact_form_is_used()
{
$this->withExceptionHandling();
$user = factory(User::class)->create();
$this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
$this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
$data = [
'name' => 'John Doe',
'email' => 'email@email.com',
'message' => 'This is a test message'
];
$notification = new ContactRequestNotification($data);
$this->mailer->shouldReceive('send')->once()->with(
['html' => 'htmlContent', 'text' => 'textContent'],
array_merge($notification->toMail($user)->toArray(), [
'__laravel_notification' => get_class($notification),
'__laravel_notification_queued' => false,
]),
m::on(function ($closure) {
$message = m::mock(Message::class);
$message->shouldReceive('to')->once()->with([$user->email]);
$closure($message);
return true;
})
);
$response = $this->post('/contact', $data);
$response->assertStatus(200);
}
}