Testing Markdown Content at Laravel 5.8

Abel Pastor
May 28 · 1 min read

After looking for some information on Google, I have noticed there is not much information about how to test the Markdown templates you can create in Laravel for Notifications or Mails. So I have decided to post it here in case it can help to some people :).

Note: If you want to test that a Notification or a Mailable has been sent, then please read Laravel docs they explain it very well.

Now how can we test that our Markdown content is accurate given the right parameters? The best way I have found is rendering the Markdown view within the test, and assert the view contains what you are looking for.

Let us imagine we have a Markdown view for an Email Notification at emails.notification (emails/notification.blade.php):

# User Active

{{ $user->name }}

Within our tests we can use the Facade “Facades\Illuminate\Mail\Markdown” to render the view and then test that it contains what we want.

In this case we want to test that the view will show up the name of the user that we pass as a parameter:

$markdown = Markdown::render(‘emails.notification’, [‘user’ => $user]);

As a first parameter we pass the markdown view we want to render, and as second parameter we pass the variables we want to use within our view.

Finally we can test the content using the function assertContains():

$this->assertContains($user->name, $markdown->toHtml());

I hope this could be helpful for someone.

Abel Pastor

Written by

Web Developer and Designer. Laravel and VueJS lover. Linux and Mac User. Always learning.