我正在为使用Mail::queue函数发送电子邮件的代码编写单元测试,就像文档中的https://laravel.com/docs/5.4/mocking#mail-fake一样
我的测试:
/** @test */
public function send_reminder()
{
Mail::fake();
$response = $this->actingAs($this->existing_account)->json('POST', '/timeline-send-reminder', []);
$response->assertStatus(302);
Mail::assertSent(ClientEmail::class, function ($mail) use ($approver) {
return $mail->approver->id === $approver->id;
});
}
正在测试的代码:
Mail::to($email, $name)->queue(new ClientEmail(Auth::user()));
错误消息:
The expected [App\Mail\ClientEmail] mailable was not sent.
Failed asserting that false is true.
该电子邮件是在我手动测试时发送的,而不是从单元测试发送的。我在想这可能是因为我使用的是
Mail::queue
而不是Mail::send
函数。在
.env
文件中,我有QUEUE_DRIVER=sync and MAIL_DRIVER=log
如何测试Laravel的
Mail::queue
? 最佳答案
找到了解决方案:
问题在于该类未导入PHPUnit测试文件的顶部。
导入邮件类解决了该问题。奇怪的是它没有使测试用例本身出错。
关于Laravel 5.4在Mail::queue,Mail::assertSent中使用伪造的Mail,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45009302/