Level 104
Try to build the mail:
Mail::assertSent(OrderCompleted::class, function($mail) {
$mail = $mail->build();
return count($mail->attachments) > 0;
});
9 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to add a feature test that checks whether a sent mailable has an attachment (for now only if it has one at all). The test looks like this
/**
* @test
*/
public function the_notification_mail_has_an_attachment()
{
Mail::fake();
$order = factory(Order::class)->create();
$order->is_completed = true;
$order->save();
Mail::assertSent(OrderCompleted::class, function($mail) {
return count($mail->attachments) > 0;
});
}
Unfortunately, the attachments array is always empty, the same goes for the rawAttachments array. There should be an attachment though, because the code inside the build method of the OrderCompleted mailable adds one:
return $this->from(config('mail.from.address'))
// ...
->attachData($generated_file, 'Test.txt', ['mime' => 'text/plain']);
So what am I missing? The docs don't give any advice concerning testing mailables for their attachments.
Try to build the mail:
Mail::assertSent(OrderCompleted::class, function($mail) {
$mail = $mail->build();
return count($mail->attachments) > 0;
});
Please or to participate in this conversation.