我正在为使用 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 fake 和 Mail::queue,Mail::assertSent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45009302/