SyntaxStudy
Sign Up
PHP Testing Events and Queues
PHP Intermediate 4 min read

Testing Events and Queues

Events and Queues

Laravel's Event::fake() and Queue::fake() intercept dispatches so you can assert without side effects.

Example
public function test_order_placed_event_dispatched(): void {
    Event::fake([OrderPlaced::class]);
    $this->actingAs($user)->postJson("/api/orders", $orderData)->assertCreated();
    Event::assertDispatched(OrderPlaced::class, function ($event) use ($orderData) {
        return $event->order->amount === $orderData["amount"];
    });
}
public function test_welcome_email_queued(): void {
    Queue::fake();
    User::factory()->create();
    Queue::assertPushed(SendWelcomeEmail::class);
}
Pro Tip

Event::fake() with specific event classes still lets other events fire — targeted faking.