0
0
Laravelframework~10 mins

Mocking and faking in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a mock of the User model.

Laravel
$user = $this->[1](User::class);
Drag options to blanks, or click blank then click option'
Afake
Bmock
Cspy
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fake' instead of 'mock' when creating a mock object.
Using 'create' which actually creates a real instance.
2fill in blank
medium

Complete the code to fake the mail sending in a test.

Laravel
Mail::[1]();
Drag options to blanks, or click blank then click option'
Aspy
Bmock
Csend
Dfake
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mock' which creates a mock but does not fake the mail system.
Using 'send' which actually sends mail.
3fill in blank
hard

Fix the error in the code to assert a method was called once on a mock.

Laravel
$mock->shouldReceive('process')->[1]();
Drag options to blanks, or click blank then click option'
Anever
Btimes
Conce
Dtwice
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'never' which asserts the method was not called.
Using 'times' without specifying a number.
4fill in blank
hard

Fill both blanks to fake the event and assert it was dispatched.

Laravel
Event::[1]();
Event::[2](UserRegistered::class);
Drag options to blanks, or click blank then click option'
Afake
Bdispatched
CassertDispatched
Dspy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dispatched' instead of 'assertDispatched' for assertion.
Using 'spy' instead of 'fake' to fake events.
5fill in blank
hard

Fill all three blanks to mock a repository, set expectation, and call the method.

Laravel
$repo = $this->[1](UserRepository::class);
$repo->shouldReceive('[2]')->andReturn(true);
$result = $repo->[3]();
Drag options to blanks, or click blank then click option'
Amock
Bsave
Dfake
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fake' instead of 'mock' to create the mock.
Using different method names in expectation and call.