Bird
Raised Fist0
Laravelframework~20 mins

HTTP test assertions in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Laravel HTTP 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 HTTP test assertion check?
Consider this Laravel test code snippet:
$response = $this->get('/home');
$response->assertStatus(200);

What does the assertStatus(200) method verify in this test?
Laravel
$response = $this->get('/home');
$response->assertStatus(200);
AIt verifies that the response contains the text '200'.
BIt checks that the HTTP response status code is exactly 200 (OK).
CIt asserts that the response has a JSON body with a 'status' key equal to 200.
DIt confirms that the response redirects to a URL containing '200'.
Attempts:
2 left
💡 Hint
The number inside assertStatus refers to the HTTP status code returned by the server.
📝 Syntax
intermediate
2:00remaining
Which option correctly asserts JSON structure in Laravel HTTP test?
You want to test that the JSON response contains a key 'user' with a nested key 'name'. Which assertion syntax is correct?
Laravel
$response = $this->get('/api/user');
A$response->assertJsonHas('user.name', 'John');
B$response->assertJsonContains(['user.name' => 'John']);
C$response->assertJsonPath('user.name', 'John');
D$response->assertJson(['user' => ['name' => 'John']]);
Attempts:
2 left
💡 Hint
Use assertJson with an array matching the expected JSON structure.
🔧 Debug
advanced
2:00remaining
Why does this Laravel HTTP test fail with a 500 error?
Given this test code:
$response = $this->post('/login', ['email' => 'user@example.com']);
$response->assertStatus(302);

The test fails with a 500 Internal Server Error instead of 302 redirect. What is the most likely cause?
Laravel
$response = $this->post('/login', ['email' => 'user@example.com']);
$response->assertStatus(302);
AThe assertStatus method is used incorrectly; it should be assertRedirect.
BThe route '/login' does not exist, so the server returns 500.
CThe POST request is missing the required 'password' field, causing a server error.
DThe test client does not support POST requests, causing the error.
Attempts:
2 left
💡 Hint
Check if all required form fields are included in the POST data.
❓ state_output
advanced
2:00remaining
What is the value of $response->headers->get('Content-Type') after this test?
In a Laravel test:
$response = $this->get('/api/data');
$contentType = $response->headers->get('Content-Type');

If the API returns JSON, what is the expected value of $contentType?
Laravel
$response = $this->get('/api/data');
$contentType = $response->headers->get('Content-Type');
A'application/json; charset=utf-8'
B'text/html; charset=UTF-8'
C'application/xml'
D'text/plain'
Attempts:
2 left
💡 Hint
APIs returning JSON usually set Content-Type to application/json.
🧠 Conceptual
expert
2:00remaining
Which Laravel HTTP test assertion ensures a redirect to a named route?
You want to test that a response redirects to a route named 'dashboard'. Which assertion method and argument correctly verify this?
Laravel
$response = $this->post('/login', ['email' => 'user@example.com', 'password' => 'secret']);
A$response->assertRedirect(route('dashboard'));
B$response->assertLocation('dashboard');
C$response->assertRedirect('/dashboard');
D$response->assertRouteIs('dashboard');
Attempts:
2 left
💡 Hint
Use the route() helper to get the URL for a named route.

Practice

(1/5)
1. Which Laravel HTTP test assertion checks if the response status code is 200?
easy
A. $response->assertStatus(200);
B. $response->assertRedirect('/home');
C. $response->assertSee('Welcome');
D. $response->assertJson(['success' => true]);

Solution

  1. Step 1: Understand the purpose of assertStatus

    This method checks the HTTP status code returned by the response.
  2. Step 2: Match the status code 200 with the assertion

    $response->assertStatus(200); confirms the response was successful (OK).
  3. Final Answer:

    $response->assertStatus(200); -> Option A
  4. Quick Check:

    Check status code = assertStatus(200) [OK]
Hint: Use assertStatus(200) to check for OK response quickly [OK]
Common Mistakes:
  • Confusing assertRedirect with assertStatus
  • Using assertSee to check status code
  • Expecting assertJson to check status
2. Which of the following is the correct syntax to assert a redirect to the URL '/dashboard' in a Laravel HTTP test?
easy
A. $response->assertLocation('/dashboard', 200);
B. $response->redirect('/dashboard');
C. $response->assertRedirect('/dashboard');
D. $response->assertStatus(302, '/dashboard');

Solution

  1. Step 1: Identify the correct assertion method for redirects

    Laravel uses assertRedirect() to check if the response redirects to a given URL.
  2. Step 2: Confirm the syntax matches the method signature

    The correct syntax is $response->assertRedirect('/dashboard'); which asserts a redirect to '/dashboard'.
  3. Final Answer:

    $response->assertRedirect('/dashboard'); -> Option C
  4. Quick Check:

    Redirect assertion = assertRedirect(url) [OK]
Hint: Use assertRedirect('url') to check redirects in tests [OK]
Common Mistakes:
  • Using assertStatus with URL parameter
  • Calling redirect() instead of assertRedirect()
  • Using assertLocation with incorrect status code
3. Given the following test code:
$response = $this->get('/welcome');
$response->assertStatus(200);
$response->assertSee('Hello World');

What will happen if the page '/welcome' returns status 404?
medium
A. The test will fail on assertSee because the content is missing.
B. The test will pass because assertSee ignores status codes.
C. The test will throw a syntax error.
D. The test will fail on assertStatus(200) because the status is 404.

Solution

  1. Step 1: Analyze the response status code

    The response returns 404, which means 'Not Found', not 200.
  2. Step 2: Understand the effect of assertStatus(200)

    This assertion expects a 200 status code, so it will fail when the actual status is 404.
  3. Final Answer:

    The test will fail on assertStatus(200) because the status is 404. -> Option D
  4. Quick Check:

    Status mismatch causes assertStatus failure [OK]
Hint: Check status code first; assertStatus fails if codes differ [OK]
Common Mistakes:
  • Assuming assertSee passes regardless of status
  • Expecting syntax errors from valid code
  • Ignoring the importance of status code in tests
4. You wrote this test:
$response = $this->post('/login', ['email' => 'user@example.com', 'password' => 'secret']);
$response->assertRedirect('/dashboard');
$response->assertStatus(200);

Why does this test fail even though the redirect is correct?
medium
A. Because the post method should be get for redirects.
B. Because assertStatus(200) conflicts with the redirect status code 302.
C. Because the URL '/dashboard' is incorrect.
D. Because assertRedirect must come after assertStatus.

Solution

  1. Step 1: Understand HTTP redirect status codes

    Redirect responses usually have status code 302, not 200.
  2. Step 2: Check the conflict between assertions

    assertRedirect('/dashboard') expects a 302 status, but assertStatus(200) expects 200, causing failure.
  3. Final Answer:

    Because assertStatus(200) conflicts with the redirect status code 302. -> Option B
  4. Quick Check:

    Redirect status is 302, not 200 [OK]
Hint: Redirects use 302 status; don't assert 200 with assertRedirect [OK]
Common Mistakes:
  • Mixing HTTP methods for redirect tests
  • Assuming assertRedirect changes status code
  • Thinking assertion order affects results
5. You want to test that a JSON API response contains a key 'status' with value 'success' and also returns HTTP status 201. Which assertion combination is correct?
hard
A. $response->assertStatus(201)->assertJson(['status' => 'success']);
B. $response->assertJson(['status' => 'success'])->assertRedirect(201);
C. $response->assertStatus(200)->assertJson(['status' => 'success']);
D. $response->assertSee('success')->assertStatus(201);

Solution

  1. Step 1: Identify the correct status code assertion

    HTTP 201 means 'Created', so assertStatus(201) is needed.
  2. Step 2: Use assertJson to check JSON content

    assertJson(['status' => 'success']) verifies the JSON key and value.
  3. Step 3: Combine assertions correctly

    Chaining assertStatus(201) and assertJson([...]) is valid and checks both conditions.
  4. Final Answer:

    $response->assertStatus(201)->assertJson(['status' => 'success']); -> Option A
  5. Quick Check:

    Status 201 + JSON key check = assertStatus + assertJson [OK]
Hint: Chain assertStatus(201) with assertJson for API success checks [OK]
Common Mistakes:
  • Using assertRedirect with status code
  • Checking status 200 instead of 201
  • Using assertSee for JSON content