Challenge - 5 Problems
Laravel Mocking & Faking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel test using a fake?
Consider this Laravel test code that fakes the mail sending. What will be the result of the assertion?
Laravel
<?php use Illuminate\Support\Facades\Mail; use Tests\TestCase; class UserTest extends TestCase { public function test_mail_is_sent() { Mail::fake(); Mail::to('user@example.com')->send(new \App\Mail\WelcomeMail()); Mail::assertSent(\App\Mail\WelcomeMail::class); } }
Attempts:
2 left
💡 Hint
Think about what Mail::fake() does and how assertSent works with it.
✗ Incorrect
Mail::fake() prevents actual emails from being sent but records the mailables sent. The assertion checks if the mail was sent, so the test passes.
❓ state_output
intermediate2:00remaining
What is the value of the mocked method call count?
Given this Laravel test using a mock, what is the value of $count after the calls?
Laravel
<?php use Illuminate\Support\Facades\Cache; use Tests\TestCase; class CacheTest extends TestCase { public function test_cache_mock() { $mock = \Mockery::mock('alias:Illuminate\Support\Facades\Cache'); $mock->shouldReceive('put')->twice()->andReturn(true); Cache::put('key1', 'value1', 10); Cache::put('key2', 'value2', 10); $count = $mock->mockery_getExpectationCount(); return $count; } }
Attempts:
2 left
💡 Hint
Check if mockery_getExpectationCount() is a valid method on the mock object.
✗ Incorrect
The method mockery_getExpectationCount() does not exist on the mock object, so calling it causes a fatal error.
🔧 Debug
advanced2:00remaining
Why does this Laravel test fail when mocking a repository?
This test tries to mock a repository method but fails with a method not found error. Why?
Laravel
<?php use Tests\TestCase; use App\Repositories\UserRepository; use Mockery; class UserRepositoryTest extends TestCase { public function test_find_user() { $mock = Mockery::mock(UserRepository::class); $mock->shouldReceive('find')->with(1)->andReturn('User1'); $result = $mock->find(1); $this->assertEquals('User1', $result); } }
Attempts:
2 left
💡 Hint
Check if the method being mocked exists and if Mockery supports mocking this class.
✗ Incorrect
Mockery can mock concrete classes and the find method exists on UserRepository. The test passes and returns the mocked value.
📝 Syntax
advanced2:00remaining
Which option correctly fakes the event dispatch in Laravel?
You want to fake event dispatching in a Laravel test. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the correct usage of Event::fake() and assertDispatched with class names.
✗ Incorrect
Event::fake() fakes all events. assertDispatched expects the event class name, not a string or partial name.
🧠 Conceptual
expert3:00remaining
What is the main difference between mocking and faking in Laravel testing?
Choose the best explanation of how mocking and faking differ in Laravel testing.
Attempts:
2 left
💡 Hint
Think about the scope and purpose of mocks versus fakes in tests.
✗ Incorrect
Mocking creates controlled stand-ins for specific methods or classes to verify interactions. Faking replaces entire services with simplified versions to avoid side effects like sending emails or dispatching events.