Laravel 5.4使用Mail假邮件与Mail :: queue,Mail :: assertSent不起作用

Sey*_*Cho 5 email unit-testing laravel laravel-5

我正在编写一个代码,用于发送带有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;
     });
}
Run Code Online (Sandbox Code Playgroud)

正在测试的代码:

Mail::to($email, $name)->queue(new ClientEmail(Auth::user()));
Run Code Online (Sandbox Code Playgroud)

错误信息:

The expected [App\Mail\ClientEmail] mailable was not sent.
Failed asserting that false is true.
Run Code Online (Sandbox Code Playgroud)

我手动测试时会发送电子邮件,但不会从单元测试中发送.我想这可能是因为我使用Mail::queue而不是Mail::send功能.

.env档案中,我有

QUEUE_DRIVER=sync and MAIL_DRIVER=log
Run Code Online (Sandbox Code Playgroud)

我怎样才能测试Mail::queueLaravel?

Sey*_*Cho 3

找到了解决方案:

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

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