Testing Markdown Content at Laravel 5.8

Abel Pastor
May 28, 2019 · 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):

@component(‘mail::message’)
# User Active

{{ $user->name }}
@endcomponent

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

More From Medium

Also tagged Notifications

Also tagged Notifications

Android Custom Notification in 6 Mins

Apr 3 · 6 min read

22

Top on Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade