0
0
Laravelframework~20 mins

HTTP test assertions in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.