Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 using Mail fake with Mail::queue, Mail::assertSent not working

I am writing unit test for a code that sends email with Mail::queue function, like the one in the documentation: https://laravel.com/docs/5.4/mocking#mail-fake

My Test:

/** @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;
     });
}

Code being Tested:

Mail::to($email, $name)->queue(new ClientEmail(Auth::user()));

Error Message:

The expected [App\Mail\ClientEmail] mailable was not sent.
Failed asserting that false is true.

The email is sent when I manually test it, but not from Unit Test. I'm thinking it might be because I am using Mail::queue instead of Mail::send function.

In .env file, I have

QUEUE_DRIVER=sync and MAIL_DRIVER=log

How can I test Mail::queue for Laravel?

like image 408
Seyong Cho Avatar asked Jul 10 '17 10:07

Seyong Cho


1 Answers

Found the solution:

The problem was that the class was not imported at the top of PHPUnit test file.

Importing the mail class solved the issue. Strange how it didn't error out the testcase itself.

like image 82
Seyong Cho Avatar answered Oct 15 '22 16:10

Seyong Cho