Lesson 8 of 9 · 22m
Testing Queues, Events, Mail, and External Integrations
Laravel fakes and HTTP client testing patterns that verify side effects without sending real mail, charging cards, or depending on flaky networks.
The important side effects in Laravel usually leave the request process: queued jobs, events, notifications, mail, and third-party HTTP calls. Testing only the HTTP 200 response misses the part that matters. Laravel's fakes exist because integration boundaries need precise assertions, not hopeful logging.
Assert work is queued, then test the job itself
public function test_report_generation_is_queued(): void
{
Queue::fake();
$this->postJson('/reports', ['month' => '2026-11'])->assertAccepted();
Queue::assertPushed(GenerateUsageReport::class, function ($job) {
return $job->month === '2026-11';
});
}That proves dispatch wiring. It does not prove the job works. Write a second test that constructs the job and calls handle() with its dependencies. Dispatch tests and execution tests answer different questions.
Events are assertions about facts
Event::fake();
$this->postJson('/invoices', $payload)->assertCreated();
Event::assertDispatched(InvoiceIssued::class, fn ($event) =>
$event->totalCents === 12000
);
Event::assertNotDispatched(InvoiceRefunded::class);Use event fakes in focused tests, not globally in every test. If you fake events while testing a listener chain end to end, you have disabled the thing you meant to verify.
Mail and notifications should be boundary-tested
Mail::fake();
$this->post('/password-reset', ['email' => 'ada@example.test']);
Mail::assertQueued(PasswordResetMail::class, fn ($mail) =>
$mail->hasTo('ada@example.test')
);For content correctness, render the mailable and inspect meaningful text. Do not snapshot the entire email HTML unless tiny template churn is intentionally painful.
Fake HTTP integrations at the client boundary
Http::fake([
'api.stripe.com/*' => Http::response(['id' => 'ch_123'], 200),
]);
$result = app(PaymentGateway::class)->charge(12000, 'pm_card_visa');
Http::assertSent(fn ($request) =>
$request->url() === 'https://api.stripe.com/v1/charges' &&
$request['amount'] === 12000
);This tests your payload construction and response handling without network flakes. For genuinely complex providers, keep a small contract suite that can run against a sandbox manually or in scheduled CI, never as a blocking test on every commit.
Test retry and failure behavior explicitly
A correct integration test suite includes the unhappy path: provider timeout, 500 response, malformed payload, and duplicate webhook delivery. Duplicate webhooks are not hypothetical. Providers retry aggressively, and your handler must be idempotent.
Field notes
Bus::fake(),Queue::fake(),Event::fake(),Mail::fake(), andNotification::fake()are different boundaries; faking one does not fake the others.- Record meaningful request attributes in HTTP assertions, not implementation details that change every refactor.
- Never point tests at a live third-party sandbox by default. CI credentials leak, sandboxes rate-limit, and flakes multiply.
- Test idempotency by performing the same action twice and asserting one external effect.
Was this lesson helpful?