Mocking and faking help you test your Laravel code by pretending parts of it work a certain way. This lets you check your code without using real services or data.
Mocking and faking in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Laravel
use Illuminate\Support\Facades\Mail; Mail::fake(); // Later in your test Mail::assertSent(YourMailable::class);
Use
::fake() on Laravel facades to replace real behavior with fake versions.Use assertions like
assertSent to check if the fake was used as expected.Examples
Laravel
use Illuminate\Support\Facades\Mail;
Mail::fake();
// Run code that sends email
Mail::assertSent(WelcomeEmail::class);Laravel
use Illuminate\Support\Facades\Http;
Http::fake([
'api.example.com/*' => Http::response(['success' => true], 200),
]);
// Run code that calls the API
Http::assertSent(function ($request) {
return $request->url() === 'https://api.example.com/data';
});Laravel
use Illuminate\Support\Facades\Storage; Storage::fake('local'); // Run code that stores files Storage::disk('local')->assertExists('file.txt');
Sample Program
This test fakes the mail system, triggers a user registration event that should send a welcome email, then checks if the email was sent without actually sending it.
Laravel
<?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Support\Facades\Mail; use App\Mail\WelcomeEmail; class WelcomeEmailTest extends TestCase { public function test_welcome_email_is_sent() { Mail::fake(); // Simulate user registration that sends welcome email event(new \App\Events\UserRegistered()); Mail::assertSent(WelcomeEmail::class); } }
Important Notes
Always fake external services in tests to keep tests fast and reliable.
Use Laravel's built-in fakes for mail, HTTP, storage, and more.
Remember to assert that the fake was used to confirm your code works as expected.
Summary
Mocking and faking let you test code without real side effects.
Laravel provides easy ways to fake mail, HTTP, storage, and other services.
Use assertions to check that your code interacted with the fakes correctly.
Practice
1. What is the main purpose of mocking and faking in Laravel testing?
easy
Solution
Step 1: Understand mocking and faking concept
Mocking and faking are used to imitate external services or actions during tests without actually performing them.Step 2: Identify the purpose in Laravel testing
Laravel uses mocking and faking to avoid real side effects like sending emails or making HTTP requests during tests.Final Answer:
To simulate external services without performing real actions -> Option BQuick Check:
Mocking = simulate services [OK]
Hint: Mocking means faking external actions in tests [OK]
Common Mistakes:
- Thinking mocking speeds up production
- Confusing mocking with real data storage
- Assuming mocking creates UI automatically
2. Which Laravel method correctly fakes sending emails in a test?
easy
Solution
Step 1: Recall Laravel's mail faking method
Laravel provides the Mail::fake() method to fake email sending during tests.Step 2: Check method correctness
Only Mail::fake() is the correct and existing method; others are invalid or do not exist.Final Answer:
Mail::fake(); -> Option AQuick Check:
Mail::fake() = correct syntax [OK]
Hint: Use Mail::fake() to fake emails in Laravel tests [OK]
Common Mistakes:
- Using non-existent methods like Mail::sendFake()
- Confusing mock() with fake()
- Forgetting the parentheses ()
3. What will the following Laravel test code output?
use Illuminate\Support\Facades\Http;
Http::fake();
$response = Http::get('https://example.com/api');
return $response->status();medium
Solution
Step 1: Understand Http::fake() behavior
Http::fake() fakes all HTTP requests and returns a default successful response with status 200.Step 2: Analyze the returned status code
The $response->status() returns 200 because the fake response simulates success.Final Answer:
200 -> Option DQuick Check:
Http::fake() returns 200 status [OK]
Hint: Http::fake() returns 200 status by default [OK]
Common Mistakes:
- Assuming fake returns 404 or error
- Expecting null instead of response object
- Confusing Http::fake() with real HTTP call
4. Identify the error in this Laravel test code snippet:
use Illuminate\Support\Facades\Storage;
Storage::fake('local');
Storage::put('file.txt', 'Hello');
$this->assertTrue(Storage::exists('file.txt'));medium
Solution
Step 1: Understand Storage::fake() usage
Storage::fake('local') fakes the 'local' disk, which is Laravel's default disk.Step 2: Check method calls for disk specification
Storage::put() and Storage::exists() use the default disk ('local'), so they interact with the faked disk correctly. The assertion passes.Final Answer:
No error, the code is correct -> Option CQuick Check:
Default disk = 'local' [OK]
Hint: Storage:: methods use default disk after Storage::fake('local') [OK]
Common Mistakes:
- Thinking disk must always be explicitly specified
- Believing Storage::put() is disallowed after fake
- Confusing required disk names
5. You want to test that your Laravel app sends exactly one notification to a user without actually sending it. Which approach correctly achieves this?
hard
Solution
Step 1: Choose the correct faking method for notifications
Notification::fake() prevents real notifications from sending and allows assertions.Step 2: Use the right assertion to check one notification sent
assertNotificationSent() verifies that a notification was sent exactly once to the user.Final Answer:
Use Notification::fake() and then assertNotificationSent() -> Option AQuick Check:
Notification::fake() + assertNotificationSent() = test notifications [OK]
Hint: Fake notifications, then assertNotificationSent() to check sends [OK]
Common Mistakes:
- Using assertNothingSent() when expecting a notification
- Trying to check notifications via database records
- Using non-existent Notification::mock() method
