Testing Laravel mailable content

This has been a thorny issue for a while, and *may* have been resolved in Laravel 9 (have not upgraded projects to 9 yet). Testing both that an email was triggered in a Laravel test, as well as verifying the content of the mail body – that has often seemed to be a tricky step. If it’s resolved in modern times… great. If not (or you’re dealing with legacy code to test), read on.

public function testCustomerOrderedItems()
{

    $customer = Customer::factory()->create();
    $item = Item::factory()->create();
    $service = $this->app->make(OrderService::class);
    $service->order($item, $customer);

    Mail::assertSent(ItemOrdered::class, function ($mail) use ($customer, $item) {
        $mail->build();
        $markdown = $this->app->make(Markdown::class);
        $body = $markdown->renderText($mail->view, $mail->buildViewData())->toHtml();
        $this->assertStringContainsString('Ordered', $body);
        $this->assertStringContainsString($item->number, $body);

        return $mail->hasTo($customer->email);
    });

} 

Of course, I can’t specifically remember where I discovered this series of steps, so apologies to whomever I may have copied this from.

A couple of things to keep in mind…

1. This only works with Markdown mailable. I’ve not used anything else during testing, so… if you’re using something else, you’re on your own here.
2. The $mail->view – this is a public property I needed to add to mailables (like ItemOrdered mailable) to be able to reference in the tests. It’s the name of the markdown/blade file – item.ordered.blade, for example.

Similar Posts

  • Identity and habits

    “Your present identity should not constrain your future habits”. For this quote from the audio book “Hello, Habits“. Obviously the book is about ‘habits’, but the phrase could easily have been “Your present identity should not constrain your future self”. In either reading of this, it’s been stuck in my head for while now.  How…

  • Laravel down migrations

    I get an email newsletter from Martin Joo every week or so. The newsletters generally have some useful tips around the Laravel framework or sometimes just general development tips. I’ve learned a couple of neat tricks here and there, and will continue to receive. This morning I received an email with a Laravel ‘tip’ regarding…

  • MySQL speed boost

    I hit a problem the other day with concurrent queries causing deadlocks.  Using innodb gives you a lot of protection with respect to transaction support, but it carries a moderate amount of overhead, and unless you’re aware of what’s going on, you may be paying a higher price which can eventually cause performance or deadlock…

  • Open Source TechFinder

    Inspired by the AUTM conference, I got inspired to look at some of the common processes techtransfer folks do. Main idea was to try to develop something relatively ‘standalone’ that might address a use case I learned about, so I decided on building a web-based open source techfinder tool to publish licensable technologies. The notion…

Leave a Reply

Your email address will not be published. Required fields are marked *