Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.3 - Avoid sending slack notification in phpunit test

So I've got a test set up that visits the sign up page and submits the form. Then the test verifies the account by token.

Once the account has been verified, a notification is sent through a SlackMessage.

What I was wondering is how can I intercept this, and stop the slack message from actually sending - only when running a test, but get some kind of confirmation that the notification did in fact get called.

like image 491
Pistachio Avatar asked Aug 25 '16 06:08

Pistachio


2 Answers

I found an undocumented method expectsNotifications() in the Laravel 5.3 MockApplicationServices trait. It works pretty much the same as the documented event mocking.

Usage is:

$this->expectsNotification($notifiable, $notification);

// eg.
$this->expectsNotification($user, UpperLimitExceeded::class);

There is also $this->withoutNotifications() to skip over any encountered notifications.

like image 140
wheeler Avatar answered Nov 16 '22 00:11

wheeler


If you need to skip sending all notifications, put the following line at the beginning of your test method:

$this->withoutNotifications();

like image 2
Robert Trzebiński Avatar answered Nov 16 '22 00:11

Robert Trzebiński