Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fake' instead of 'mock' when creating a mock object.
Using 'create' which actually creates a real instance.
✗ Incorrect
Use mock to create a mock object in Laravel for testing.
2fill in blank
mediumComplete the code to fake the mail sending in a test.
Laravel
Mail::[1](); Drag options to blanks, or click blank then click option'
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.
✗ Incorrect
Use fake to prevent actual mail sending during tests.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'never' which asserts the method was not called.
Using 'times' without specifying a number.
✗ Incorrect
Use once() to assert the method was called exactly one time.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dispatched' instead of 'assertDispatched' for assertion.
Using 'spy' instead of 'fake' to fake events.
✗ Incorrect
Use fake() to fake events and assertDispatched() to check an event was fired.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fake' instead of 'mock' to create the mock.
Using different method names in expectation and call.
✗ Incorrect
First, create a mock with mock(). Then set expectation on save method and call it.