Обычно , если я хочу протестировать поведение прослушивателя событий, я бы сделал что-то вроде этого:
- Создайте событие, которое должен обработать прослушиватель.
- Создайте экземпляр прослушивателя событий и вызовите его метод handle для события.
- Утвердите, что действия (в этом пример отправки уведомления) выполняются правильно.
Код: Выделить всё
public function test_sends_notification_to_newly_created_user(): void
{
$user = User::factory()->create();
$event = new UserCreated($user);
$eventListener = $this->app->make(SendUserCreationNotification::class);
$eventListener->handle($event);
Notification::assertSentTo($user, WelcomeNotification::class);
}
Код: Выделить всё
public function handle(JobQueued $event): void
{
$jobUuid = $event->payload()['uuid'];
if (property_exists($event->job, 'trackableJob')) {
$this->updater->setQueuedStatus($event->job->trackableJob, $jobUuid);
}
}
Код: Выделить всё
public function test_updates_status_to_queued_in_database(): void
{
$event = new JobQueued(...); // I have to manually specify the payload (don't want to do that)
}
Код: Выделить всё
public function test_updates_status_to_queued_in_database(): void
{
app(Dispatcher::class)->dispatch(new TrackedJob());
$this->artisan('queue:work --once');
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... nt-in-test