Challenge - 5 Problems
Laravel Unit Test 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 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);
}Attempts:
2 left
💡 Hint
Remember that make() creates an instance without saving it.
✗ Incorrect
The factory's make() method creates a User instance with the given email but does not save it. The assertEquals checks the email property correctly, so the test passes.
📝 Syntax
intermediate1:30remaining
Which option correctly defines a Laravel unit test method?
Identify the correct syntax for a Laravel unit test method inside a test class.
Attempts:
2 left
💡 Hint
Test methods must be public functions with parentheses.
✗ Incorrect
In Laravel, test methods must be public functions with parentheses and a valid name. Option A follows this pattern correctly.
🔧 Debug
advanced2: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]);
}Attempts:
2 left
💡 Hint
Check the difference between make() and create() in Laravel factories.
✗ Incorrect
The make() method creates a model instance without saving it. Therefore, the database does not have the user record, causing assertDatabaseHas to fail.
❓ state_output
advanced1: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();
}Attempts:
2 left
💡 Hint
Factories with create() save records to the database.
✗ Incorrect
The factory creates and saves 3 users, so User::count() returns 3.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how to keep tests isolated and avoid leftover data.
✗ Incorrect
RefreshDatabase runs migrations before each test, resetting the database to a clean state to avoid data conflicts between tests.