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
Recall & Review
beginner
What is the purpose of HTTP test assertions in Laravel?
HTTP test assertions check if your web application's responses behave as expected during automated tests. They help verify status codes, content, redirects, and more.
Click to reveal answer
beginner
How do you assert that a response status is 200 OK in Laravel tests?
Use $response->assertStatus(200); to check if the HTTP response status code is 200, meaning the request was successful.
Click to reveal answer
beginner
What does $response->assertSee('text') do in Laravel HTTP tests?
It checks if the response content contains the given text string. This helps confirm that specific content is visible in the response.
Click to reveal answer
intermediate
How can you test that a response redirects to a specific URL in Laravel?
Use $response->assertRedirect('/target-url'); to verify the response sends a redirect to the given URL.
Click to reveal answer
intermediate
What assertion would you use to check if a JSON response contains a specific structure or data?
Use $response->assertJson(['key' => 'value']); to verify the JSON response includes the specified key-value pairs.
Click to reveal answer
Which Laravel assertion checks if the HTTP response status is 404?
A$response->assertJson(['status' => 404]);
B$response->assertSee('404');
C$response->assertRedirect(404);
D$response->assertStatus(404);
✗ Incorrect
assertStatus(404) verifies the HTTP status code is 404 Not Found.
How do you verify that a response contains the text 'Welcome' in Laravel tests?
A$response->assertContains('Welcome');
B$response->assertSee('Welcome');
C$response->assertText('Welcome');
D$response->assertHas('Welcome');
✗ Incorrect
assertSee('Welcome') checks if the response content includes the text 'Welcome'.
Which assertion confirms a response redirects to '/home'?
A$response->assertRedirect('/home');
B$response->assertStatus(302);
C$response->assertLocation('/home');
D$response->assertJson(['redirect' => '/home']);
✗ Incorrect
assertRedirect('/home') checks the response redirects to the URL '/home'.
To check if a JSON response has a key 'user' with value 'admin', which assertion is correct?
A$response->assertJsonContains('user', 'admin');
B$response->assertSeeJson('user', 'admin');
C$response->assertJson(['user' => 'admin']);
D$response->assertJsonHas('user', 'admin');
✗ Incorrect
assertJson(['user' => 'admin']) verifies the JSON response includes that key-value pair.
Which assertion would you use to confirm a response has no errors in Laravel?
A$response->assertSuccessful();
B$response->assertNoErrors();
C$response->assertOk();
D$response->assertValid();
✗ Incorrect
assertSuccessful() checks if the response status code is between 200 and 299, meaning no errors.
Explain how to use Laravel HTTP test assertions to verify a page loads successfully and contains specific text.
Think about checking status and visible content.
You got /3 concepts.
Describe how to test JSON responses in Laravel using HTTP test assertions.
Focus on JSON content verification.
You got /3 concepts.
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
Step 1: Understand the purpose of assertStatus
This method checks the HTTP status code returned by the response.
Step 2: Match the status code 200 with the assertion
$response->assertStatus(200); confirms the response was successful (OK).
Final Answer:
$response->assertStatus(200); -> Option A
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
Step 1: Identify the correct assertion method for redirects
Laravel uses assertRedirect() to check if the response redirects to a given URL.
Step 2: Confirm the syntax matches the method signature
The correct syntax is $response->assertRedirect('/dashboard'); which asserts a redirect to '/dashboard'.
Final Answer:
$response->assertRedirect('/dashboard'); -> Option C
Quick Check:
Redirect assertion = assertRedirect(url) [OK]
Hint: Use assertRedirect('url') to check redirects in tests [OK]
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
Step 1: Understand HTTP redirect status codes
Redirect responses usually have status code 302, not 200.
Step 2: Check the conflict between assertions
assertRedirect('/dashboard') expects a 302 status, but assertStatus(200) expects 200, causing failure.
Final Answer:
Because assertStatus(200) conflicts with the redirect status code 302. -> Option B
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
Step 1: Identify the correct status code assertion
HTTP 201 means 'Created', so assertStatus(201) is needed.
Step 2: Use assertJson to check JSON content
assertJson(['status' => 'success']) verifies the JSON key and value.
Step 3: Combine assertions correctly
Chaining assertStatus(201) and assertJson([...]) is valid and checks both conditions.
Final Answer:
$response->assertStatus(201)->assertJson(['status' => 'success']); -> Option A