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

  • 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…

  • 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…

  • A bit of feedback…

    A small bit of feedback… that’s often what a mobile user is looking for. Haptic feedback – a quick device vibration – is great. It’s subtle, quick, doesn’t interrupt, but gives an actual *feeling* that something happened. And… on iOS, it’s harder to do without building a full ‘native mobile app’. iOS Safari doesn’t support…

  • k as in knife

    Many moons ago my earlier version of my blog had this list I use when spelling things for people over the phone. I managed to find it at archive.org and thought I’d repost… a as in aisle (or aye) b as in bdellium  c as in czar d as in djibouti e as in eight…

  • Importance of backups

    Well… here we are.  10 years later, and … no backups.  Or… none of the data that’s important. Recently had a drive crash in my main server where this blog is hosted.  Had it happen 2 years ago, but the data was recovered, and I put everything on automatic backups.  Using virtualmin, a great control…

Leave a Reply

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