1 min read

Testing mail attachments in Laravel

Laravel makes it very easy to test internal components such as Mailables, Queues, Events, and everything in between.

Testing that a mailable got sent is very straight forward. See the following example:

// ExampleTest.php

// If the mailable isn't queued
Mail::assertSent(UserWelcomeEmail::class);

// if the mailable is queued.
Mail::assertQueued(UserWelcomeEmail::class);

Testing that an email has x number of attachments is also super easy. Take this as an example:

Mail::assertQueued(InvoiceGenerated::class, function ($mail) {
    $mail = $mail->build();
    $this->assertCount(1, $mail->diskAttachments);

    return true;
});