Summer Sale! All accounts are 50% off this week.

Synchro's avatar

Mail::fake() breaking tests

I'm using Mail::fake() to allow me to capture mail sending events during tests, and I have seen it work, however, I'm finding it sometimes causes a weird breakage. If I cut back my code to this:

$mail = Mail::to($user);
$mail->bcc('[email protected]')
    ->send(new Notify($user))

the email is sent correctly. If I now wrap that with Mail::fake() and an assertion:

Mail::fake();
$mail = Mail::to($user);
$mail->bcc('[email protected]')
    ->send(new Notify($user));
Mail::assertSent(Notify::class);

it fails on the $mail->bcc() call saying that $mail is null, and if I inspect it in a debugger, it is. The $user variable does contain a valid User instance, and it does have a populated email property. It behaves the same way if I pass an email address as a simple string.

Note that it does the same if I chain the call onto the initial to() call; I've only broken out the $mail variable to make it easier to get at in a debugger.

Why would Mail::fake be breaking in this way? FWIW I'm in Laravel 8.70 on PHP 8.0.12.

0 likes
0 replies

Please or to participate in this conversation.