0
0
Laravelframework~20 mins

Mocking and faking in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Mocking & Faking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
    }
}
AThe test fails because Mail::fake() disables sending mails and no mail is sent.
BThe test passes because the mail was sent and caught by the fake.
CThe test throws an error because Mail::assertSent() cannot be used after Mail::fake().
DThe test passes but Mail::assertSent() always returns true regardless of mails sent.
Attempts:
2 left
💡 Hint
Think about what Mail::fake() does and how assertSent works with it.
state_output
intermediate
2: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;
    }
}
A0
B1
CThrows an error because mockery_getExpectationCount() is not a method
D2
Attempts:
2 left
💡 Hint
Check if mockery_getExpectationCount() is a valid method on the mock object.
🔧 Debug
advanced
2: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);
    }
}
AThe test passes without errors and returns 'User1' as expected.
BThe test fails because the find method does not exist on UserRepository, so Mockery cannot mock it.
CThe test fails because Mockery requires calling Mockery::close() after the test to avoid errors.
DThe test fails because UserRepository is a concrete class and Mockery cannot mock concrete classes by default.
Attempts:
2 left
💡 Hint
Check if the method being mocked exists and if Mockery supports mocking this class.
📝 Syntax
advanced
2: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?
A
Event::fake();
Event::assertDispatched(UserRegistered::class);
B
Event::fake(UserRegistered::class);
Event::assertDispatched(UserRegistered::class);
C
Event::fake();
Event::assertDispatched('UserRegistered');
D
Event::fake();
Event::assertDispatched('App\Events\UserRegistered');
Attempts:
2 left
💡 Hint
Check the correct usage of Event::fake() and assertDispatched with class names.
🧠 Conceptual
expert
3: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.
AMocking disables all functionality of a class, while faking allows the original methods to run but logs calls.
BMocking is only used for database queries, while faking is used for HTTP requests and events.
CMocking and faking are the same in Laravel; both replace real implementations with test doubles.
DMocking replaces a class or method with a dummy that records calls and can return custom values, while faking replaces a whole service with a simplified version that simulates behavior without side effects.
Attempts:
2 left
💡 Hint
Think about the scope and purpose of mocks versus fakes in tests.