Skip to content
Advertisement

Laravel Notification Mailable not working in Test

I’am using a notification to send a mail to all users when the notification gets Triggered.

This works fine in manual “browsing the website” usage (submit a POST to the ContactController) but in my tests the mail facade claims that no mail gets sent.

Controller:

JavaScript

Notification:

JavaScript

Mailable:

JavaScript

Test:

JavaScript

The “a_notification_gets_created_when_the_contact_form_is_used” test runs without any problems but the second test case results in:

JavaScript

When performing the same tasks in the browser a mail gets sent to the user.

Any tips on what i am missing here? An internet search revealed that some other people had this problem but neither found a solution (or that I am not capable of performing the correct search)

Thanks!

Advertisement

Answer

It looks like the MailChannel driver that sends notification emails doesn’t use the Mail facade which means Mail::fake wouldn’t affect it. Instead it calls the send method on the Mailable directly which in turns calls send on the Mailer (mail driver).

You could have replaced the Mailable instance with an instance of MailFake (which is what Mail::fake uses), but it looks like MailFake doesn’t cater for the case when the $view is an array (which is what the MailChannel passes to Mailable).

Luckily the Laravel source contains an example of how they test notifications that send mails in the SendingMailNotificationsTest. They mock the Mailer and Markdown instances and check the parameters that get passed. You could do something similar like:

JavaScript

Personally, I would rather just unit test the toMail method on the ContactRequestNotification class for now because I don’t think the method above is very pretty.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement