Testing with Notification::fake()
Test notification dispatches effortlessly without sending real emails, SMS messages, or network requests.
Swapping with Notification::fake()
Call Notification::fake() at the start of your test. It replaces the real delivery engine with an in-memory double that records all dispatches:
php
use App\Notifications\OrderConfirmed;
use Jengo\Notifications\Notification;
test('orders trigger confirmation notification', function () {
Notification::fake();
$user = User::factory()->create();
$order = ['id' => 42, 'amount' => 199.99];
// Trigger application logic that sends notification
$user->notify(new OrderConfirmed($order));
// Assert notification was sent to the user
Notification::assertSentTo($user, OrderConfirmed::class);
});Available Test Assertions
assertSentTo
Asserts that a notification was dispatched to a specific recipient. Accepts an optional callback to verify payload data:
php
Notification::assertSentTo(
$user,
OrderConfirmed::class,
function (OrderConfirmed $notification, array $channels) use ($order) {
return $notification->order['id'] === $order['id']
&& in_array('mail', $channels, true);
}
);assertNotSentTo
Asserts that a specific notification was NOT dispatched to a recipient:
php
Notification::assertNotSentTo($unsubscribedUser, PromotionalOffer::class);assertSentOnChannel
Asserts that a notification was sent via a specific delivery channel:
php
Notification::assertSentOnChannel('sms', OrderConfirmed::class);
Notification::assertSentOnChannel('database', OrderConfirmed::class);assertSentTimes
Asserts that a notification was sent a specific number of times across all recipients:
php
Notification::assertSentTimes(OrderConfirmed::class, 1);assertNothingSent
Asserts that no notifications were dispatched:
php
Notification::assertNothingSent();Testing On-Demand Routing
You can also assert that notifications were sent to ad-hoc recipients routed via Notification::route():
php
use Jengo\Notifications\Support\AnonymousNotifiable;
test('it routes alert to emergency webhook', function () {
Notification::fake();
Notification::route('webhook', 'https://ops.company.com/alerts')
->notify(new SecurityAlert('Root login detected'));
Notification::assertSentTo(
new AnonymousNotifiable(),
SecurityAlert::class,
function ($notification, $channels, $notifiable) {
return $notifiable->routes['webhook'] === 'https://ops.company.com/alerts';
}
);
});