Laravel 5.4 使用 Mail fake 与 Mail::queue, Mail::assertSent 不起作



我正在为使用 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 [AppMailClientEmail] mailable was not sent.
Failed asserting that false is true.

电子邮件是在我手动测试时发送的,而不是从单元测试发送的。我想这可能是因为我使用的是Mail::queue而不是Mail::send函数。

.env文件中,我有

QUEUE_DRIVER=sync and MAIL_DRIVER=log

如何测试拉拉维尔的Mail::queue

找到了解决方案:

问题是该类未在 PHPUnit 测试文件的顶部导入。

导入邮件类解决了这个问题。奇怪的是它如何没有错误测试用例本身。

最新更新