Discover how pretending services work can save hours of testing headaches!
Why Mocking and faking in Laravel? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine testing your Laravel app that sends emails or talks to a payment service. You have to wait for real emails or real payments every time you run tests.
Manually waiting for real services makes tests slow and flaky. Sometimes external services fail or cost money. It's hard to test all cases reliably.
Mocking and faking let you pretend those services work perfectly or fail on purpose. This way, tests run fast and always behave the same.
$response = $this->post('/pay', ['amount' => 100]); // actually calls payment API $this->assertTrue($response->successful());
Payment::fake(); $response = $this->post('/pay', ['amount' => 100]); Payment::assertCharged(100);
It enables fast, reliable tests that simulate real-world scenarios without depending on external services.
When testing user registration, you fake sending a welcome email instead of sending a real one, so tests run instantly and don't spam inboxes.
Manual testing with real services is slow and unreliable.
Mocking and faking simulate services for fast, stable tests.
This helps catch bugs early without extra costs or delays.
Practice
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]
- Thinking mocking speeds up production
- Confusing mocking with real data storage
- Assuming mocking creates UI automatically
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]
- Using non-existent methods like Mail::sendFake()
- Confusing mock() with fake()
- Forgetting the parentheses ()
use Illuminate\Support\Facades\Http;
Http::fake();
$response = Http::get('https://example.com/api');
return $response->status();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]
- Assuming fake returns 404 or error
- Expecting null instead of response object
- Confusing Http::fake() with real HTTP call
use Illuminate\Support\Facades\Storage;
Storage::fake('local');
Storage::put('file.txt', 'Hello');
$this->assertTrue(Storage::exists('file.txt'));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]
- Thinking disk must always be explicitly specified
- Believing Storage::put() is disallowed after fake
- Confusing required disk names
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]
- Using assertNothingSent() when expecting a notification
- Trying to check notifications via database records
- Using non-existent Notification::mock() method
