0
0
Laravelframework~20 mins

Unit tests in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Unit Test 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 unit test?
Consider this Laravel unit test method. What will be the test result when running it?
Laravel
<?php
public function testUserCreation()
{
    $user = User::factory()->make(['email' => 'test@example.com']);
    $this->assertEquals('test@example.com', $user->email);
}
AThe test fails because the user is not saved to the database.
BThe test passes because the email matches the expected value.
CThe test throws an error because User::factory() is undefined.
DThe test fails because assertEquals is used incorrectly.
Attempts:
2 left
💡 Hint
Remember that make() creates an instance without saving it.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a Laravel unit test method?
Identify the correct syntax for a Laravel unit test method inside a test class.
Apublic function test_example() { $this->assertTrue(true); }
Bpublic testExample() { $this->assertTrue(true); }
Cfunction testExample() { $this->assertTrue(true); }
Dpublic function testExample { $this->assertTrue(true); }
Attempts:
2 left
💡 Hint
Test methods must be public functions with parentheses.
🔧 Debug
advanced
2:30remaining
Why does this Laravel unit test fail with a database error?
This test tries to create a user and check the database, but it fails with a database error. Why?
Laravel
<?php
public function testUserIsSaved()
{
    $user = User::factory()->make();
    $this->assertDatabaseHas('users', ['email' => $user->email]);
}
ABecause the test method is missing a call to refresh the database connection.
BBecause assertDatabaseHas requires a saved model instance, make() is invalid here.
CBecause the users table does not exist in the test database.
DBecause make() does not save the user to the database, so the record is missing.
Attempts:
2 left
💡 Hint
Check the difference between make() and create() in Laravel factories.
state_output
advanced
1:30remaining
What is the value of $count after this Laravel test runs?
Given this test code, what will be the value of $count after execution?
Laravel
<?php
public function testUserCount()
{
    User::factory()->count(3)->create();
    $count = User::count();
}
A1
B0
C3
DThrows an error because User::count() is not accessible.
Attempts:
2 left
💡 Hint
Factories with create() save records to the database.
🧠 Conceptual
expert
2:00remaining
Which option best describes the purpose of Laravel's RefreshDatabase trait in unit tests?
What does the RefreshDatabase trait do when used in Laravel unit tests?
AIt resets the database by running migrations before each test to ensure a clean state.
BIt caches database queries to speed up tests.
CIt disables database transactions during tests.
DIt automatically seeds the database with test data before each test.
Attempts:
2 left
💡 Hint
Think about how to keep tests isolated and avoid leftover data.