Interview frequency: …
Short answer
Use built-in fakes such as Event::fake(), Queue::fake(), or Mail::fake(), and verify sends with Mail::assertSent().
Explanation
In tests, you do not want to send real email to a user or charge a real credit card. Laravel provides built-in fakes for core Facades; they intercept operations in memory and record what happened. This lets you verify that code attempted to send the required email with the right data without causing an actual external effect.
Code example
public function test_orders_trigger_mail(): void {
Mail::fake();
// Execute the purchase operation
$this->postJson('/api/orders', $data);
// Confirmation
Mail::assertSent(OrderConfirmationMail::class);
}Common mistakes
- Calling Mail::fake() after the code runs instead of before it.
Follow-up questions
- How do you test code that calls external APIs with Http::fake() and prevent real requests?
- How do multi-channel Notifications work in Laravel, and how do you test them with Notification::fake()?