HTTP test assertions help you check if your web app responds correctly to requests. They make sure your app works as expected without opening a browser.
HTTP test assertions in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
$response->assertStatus(200); $response->assertSee('Welcome'); $response->assertRedirect('/home');
Use $response from your HTTP test calls like $this->get() or $this->post().
Assertions check things like status codes, page content, redirects, and JSON responses.
$response = $this->get('/'); $response->assertStatus(200);
$response = $this->post('/login', ['email' => 'user@example.com', 'password' => 'secret']); $response->assertRedirect('/dashboard');
$response = $this->get('/profile'); $response->assertSee('User Profile');
$response = $this->getJson('/api/data'); $response->assertJson(['success' => true]);
This test class has two tests: one checks the home page loads with status 200 and shows a welcome message. The other checks that logging in redirects the user to the dashboard page.
<?php namespace Tests\Feature; use Tests\TestCase; class HomePageTest extends TestCase { public function test_home_page_loads_successfully() { $response = $this->get('/'); $response->assertStatus(200); $response->assertSee('Welcome to Our Site'); } public function test_login_redirects_to_dashboard() { $response = $this->post('/login', [ 'email' => 'user@example.com', 'password' => 'password123', ]); $response->assertRedirect('/dashboard'); } }
Always use HTTP test assertions inside your test methods after making a request.
Use assertStatus() to check HTTP codes like 200, 404, or 302.
Use assertSee() to check if certain text appears in the response body.
HTTP test assertions help confirm your app responds correctly to requests.
Common assertions include checking status codes, redirects, and page content.
They make your tests reliable and your app more stable.
Practice
Solution
Step 1: Understand the purpose of
This method checks the HTTP status code returned by the response.assertStatusStep 2: Match the status code 200 with the assertion
$response->assertStatus(200);confirms the response was successful (OK).Final Answer:
$response->assertStatus(200); -> Option AQuick Check:
Check status code = assertStatus(200) [OK]
- Confusing assertRedirect with assertStatus
- Using assertSee to check status code
- Expecting assertJson to check status
Solution
Step 1: Identify the correct assertion method for redirects
Laravel usesassertRedirect()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 CQuick Check:
Redirect assertion = assertRedirect(url) [OK]
- Using assertStatus with URL parameter
- Calling redirect() instead of assertRedirect()
- Using assertLocation with incorrect status code
$response = $this->get('/welcome');
$response->assertStatus(200);
$response->assertSee('Hello World');What will happen if the page '/welcome' returns status 404?
Solution
Step 1: Analyze the response status code
The response returns 404, which means 'Not Found', not 200.Step 2: Understand the effect of
This assertion expects a 200 status code, so it will fail when the actual status is 404.assertStatus(200)Final Answer:
The test will fail on assertStatus(200) because the status is 404. -> Option DQuick Check:
Status mismatch causes assertStatus failure [OK]
- Assuming assertSee passes regardless of status
- Expecting syntax errors from valid code
- Ignoring the importance of status code in tests
$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?
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, butassertStatus(200)expects 200, causing failure.Final Answer:
Because assertStatus(200) conflicts with the redirect status code 302. -> Option BQuick Check:
Redirect status is 302, not 200 [OK]
- Mixing HTTP methods for redirect tests
- Assuming assertRedirect changes status code
- Thinking assertion order affects results
Solution
Step 1: Identify the correct status code assertion
HTTP 201 means 'Created', soassertStatus(201)is needed.Step 2: Use
assertJsonto check JSON contentassertJson(['status' => 'success'])verifies the JSON key and value.Step 3: Combine assertions correctly
ChainingassertStatus(201)andassertJson([...])is valid and checks both conditions.Final Answer:
$response->assertStatus(201)->assertJson(['status' => 'success']); -> Option AQuick Check:
Status 201 + JSON key check = assertStatus + assertJson [OK]
- Using assertRedirect with status code
- Checking status 200 instead of 201
- Using assertSee for JSON content
