0
0
Laravelframework~10 mins

Mocking and faking in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking and faking
Start Test
Create Fake/Mock
Inject into Code
Run Code Using Fake/Mock
Verify Behavior or Output
Test Pass/Fail
End
This flow shows how Laravel tests use mocks or fakes to replace real parts, run code, then check results.
Execution Sample
Laravel
<?php
use Illuminate\Support\Facades\Notification;

public function testUserReceivesNotification()
{
    Notification::fake();
    $user = User::factory()->create();
    $user->notify(new WelcomeNotification());
    Notification::assertSentTo($user, WelcomeNotification::class);
}
This test fakes notifications, triggers one, then checks if it was sent to the user.
Execution Table
StepActionEffectState ChangeVerification
1Call Notification::fake()Replaces real notification with fakeNotification facade now uses fakeNo notifications sent yet
2Create user with factoryUser object created in databaseUser exists in DBUser ready for notification
3Call $user->notify(new WelcomeNotification())Notification sent via fakeFake records notification sentNotification recorded but not actually sent
4Call Notification::assertSentTo($user, WelcomeNotification::class)Checks if notification was sent to userNo state changeAssertion passes if notification recorded
5Test endsTest passes if assertion trueTest completeTest result: PASS
💡 Test ends after assertion confirms notification was sent to user
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Notification facadeReal notification systemFake notification system activeFake records notification sentFake notification system active
$userNot createdUser object createdUser object existsUser object exists
Notifications sent001 (WelcomeNotification)1 (WelcomeNotification)
Key Moments - 3 Insights
Why do we call Notification::fake() before sending notifications?
Calling Notification::fake() replaces the real notification system with a fake one that records notifications instead of sending them. This is shown in execution_table step 1 and 3.
Does Notification::assertSentTo() send a notification?
No, assertSentTo() only checks if a notification was recorded as sent by the fake. It does not send anything. See execution_table step 4.
What happens if we don't fake notifications in the test?
Without faking, notifications would actually send, which is not good for tests. Also, assertSentTo() would fail because no fake records exist. This is why step 1 is important.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the Notification facade after step 2?
AIt uses the fake notification system
BIt uses the real notification system
CIt has sent one notification
DIt is uninitialized
💡 Hint
Check the 'State Change' column at step 2 in the execution_table
At which step does the test verify if the notification was sent?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Verification' column to find when assertion happens
If Notification::fake() was not called, what would happen to the test?
ATest would pass anyway
BNotification::assertSentTo() would record notifications
CNotification would be sent for real and test might fail
DUser would not be created
💡 Hint
Refer to key_moments about why faking is needed before sending notifications
Concept Snapshot
Laravel Mocking and Faking Cheat Sheet:
- Use Notification::fake() to replace real notifications with a fake.
- Run code that triggers notifications.
- Use Notification::assertSentTo() to check if notification was sent.
- Fakes record calls without side effects.
- Always fake before triggering to avoid real actions.
Full Transcript
In Laravel testing, mocking and faking help replace real services with test doubles. For example, Notification::fake() swaps the real notification system with a fake that records notifications instead of sending them. Then, when code triggers notifications, the fake records them. Finally, tests use assertions like Notification::assertSentTo() to verify notifications were sent to the right users. This approach avoids side effects and makes tests reliable. The execution flow starts with faking, then creating data, triggering notifications, asserting, and ending the test. Variables like the Notification facade switch from real to fake, and notifications sent count changes from zero to one. Key points include always faking before sending and understanding that assertions check recorded calls, not send notifications themselves.