0
0
Laravelframework~20 mins

Feature tests in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Feature Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Laravel feature test do?
Consider this Laravel feature test code. What is the expected behavior when this test runs?
Laravel
public function test_homepage_shows_welcome_message()
{
    $response = $this->get('/');
    $response->assertStatus(200);
    $response->assertSee('Welcome to our site');
}
AIt checks that the homepage URL returns a 200 status and contains the text 'Welcome to our site'.
BIt tests that the homepage redirects to another page with a welcome message.
CIt ensures the homepage returns JSON data containing a welcome message.
DIt verifies that the homepage returns a 404 error with a welcome message.
Attempts:
2 left
💡 Hint
Look at the methods get(), assertStatus(), and assertSee() to understand what is tested.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Laravel feature test
Which option correctly fixes the syntax error in this test method?
Laravel
public function test_user_can_login()
{
    $response = $this->post('/login', [
        'email' => 'user@example.com',
        'password' => 'secret'
    ]);
    $response->assertRedirect('/dashboard');
}
AChange post() to get() to fix the syntax error.
BRemove the closing brace '}' to fix the syntax error.
CAdd a semicolon after assertRedirect('/dashboard') to fix the syntax error.
DReplace the array with a JSON string to fix the syntax error.
Attempts:
2 left
💡 Hint
Check the end of the assertRedirect line for missing punctuation.
state_output
advanced
2:00remaining
What is the state of the database after this feature test runs?
Given this Laravel feature test that creates a user, what will be the number of users in the database after the test completes?
Laravel
public function test_user_creation()
{
    $this->post('/users', [
        'name' => 'Alice',
        'email' => 'alice@example.com',
        'password' => 'password123'
    ]);

    $this->assertDatabaseHas('users', ['email' => 'alice@example.com']);
}
AThe test will fail because the password is not hashed.
BThere will be multiple users with the same email due to repeated test runs.
CThere will be exactly one user with email 'alice@example.com' in the users table.
DThere will be no users in the database because tests do not persist data.
Attempts:
2 left
💡 Hint
Laravel feature tests use database transactions by default to isolate tests.
🔧 Debug
advanced
2:00remaining
Why does this Laravel feature test fail?
This test is supposed to check that a guest user is redirected to login when accessing /dashboard. Why does it fail?
Laravel
public function test_guest_redirected_to_login()
{
    $response = $this->get('/dashboard');
    $response->assertStatus(200);
    $response->assertSee('Login');
}
AThe test fails because the /dashboard route does not exist.
BThe test expects status 200 but the route redirects with status 302, causing failure.
CThe test fails because the response does not contain the word 'Login'.
DThe test fails because the get() method requires authentication.
Attempts:
2 left
💡 Hint
Check the expected HTTP status code for redirects.
🧠 Conceptual
expert
2:00remaining
Which option best describes Laravel feature tests?
Choose the statement that correctly explains the purpose and behavior of Laravel feature tests.
AFeature tests simulate user interactions with the application through HTTP requests to verify end-to-end behavior.
BFeature tests only check individual methods in models without involving HTTP requests.
CFeature tests are used to test database migrations and schema changes only.
DFeature tests run in production to monitor live user activity.
Attempts:
2 left
💡 Hint
Think about what 'feature' means in testing Laravel apps.