Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Laravel notification without notifiable?

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?

like image 683
Yogesh.galav Avatar asked Nov 05 '25 16:11

Yogesh.galav


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);
    }
}
like image 98
Maksim Avatar answered Nov 07 '25 11:11

Maksim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!