How to test Laravel notification without notifiable?

Viewed 338

We are sending a slack message to our team channel hence not using a notifiable instance. This is how I did it-

Notification::route('slack', env('SLACK_URL'))
    ->notify(new StaffNotification());

And in StaffNotification

public function toSlack() {
    return (new SlackMessage)->content('New Staff Message.');
}

How should I test StaffNotification as all the assert available are accepting the first parameter as notifiable?

1 Answers

Laravel creates an AnonymousNotifiable behind the scene when you have not a notifiable in your model, and you can use it:

use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;

class NotificationsTest extends TestCase
{
    public function testSend()
    {
        Notification::fake();
        //perform your code
        Notification::assertSentTo(new AnonymousNotifiable(), StaffNotification::class);
    }
}
Related