Level 29
Try to use assertSentTo instead of hasSent, check the docs for more info: https://laravel.com/docs/10.x/notifications#testing
Example:
$users->each(function ($user) {
Notification::assertSentTo($user, TaskDueNotification::class);
});
I don't know if I'm doing it right. I'm fairly new to testing. The test is failing with the following error message
Expected [App\Notifications\TaskDueNotification] to be sent 2 times, but was sent 0 times.
public function test_job_was_dispatched()
{
Queue::fake([NotifyUserDueTasksJob::class]);
Notification::fake();
$users = User::factory()->has(Task::factory(1)->dueToday())->count(2)->create();
NotifyUserDueTasksJob::dispatch();
Queue::assertPushed(NotifyUserDueTasksJob::class);
$users->each(function ($user) {
Notification::hasSent($user, TaskDueNotification::class);
});
Notification::assertSentTimes(TaskDueNotification::class, 2);
}
This is how the job looks
class NotifyUserDueTasksJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle(): void
{
$tasksDueToday = Task::dueToday()->with('user')->get();
$tasksDueToday->groupBy('user_id')->each(function ($tasks) {
$user = $tasks->first()->user;
$user->notify(new TaskDueNotification($user, $tasks));
});
}
}
Please or to participate in this conversation.