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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With