0
0
Laravelframework~30 mins

HTTP test assertions in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
HTTP Test Assertions in Laravel
📖 Scenario: You are building a simple Laravel application that has a route returning a welcome message. You want to write tests to check if the HTTP responses are correct.
🎯 Goal: Write HTTP test assertions in Laravel to verify the response status, content, and JSON structure of a route.
📋 What You'll Learn
Create a test method that sends a GET request to the /welcome route
Assert that the response status is 200
Assert that the response contains the text Welcome to Laravel
Assert that the response JSON has a key message with value Welcome to Laravel
💡 Why This Matters
🌍 Real World
Testing HTTP responses ensures your web routes return the correct data and status, preventing bugs before deployment.
💼 Career
Writing HTTP test assertions is a key skill for Laravel developers to maintain reliable and robust web applications.
Progress0 / 4 steps
1
Create a test method with a GET request
Create a test method called test_welcome_route_returns_success inside a Laravel test class. Inside it, send a GET request to the /welcome route using $response = $this->get('/welcome');
Laravel
Need a hint?

Use $this->get('/welcome') to send a GET request inside your test method.

2
Assert the response status is 200
Add an assertion to check that the response status is 200 using $response->assertStatus(200); inside the test_welcome_route_returns_success method.
Laravel
Need a hint?

Use assertStatus(200) on the response object to check the HTTP status.

3
Assert the response contains specific text
Add an assertion to check that the response contains the text Welcome to Laravel using $response->assertSee('Welcome to Laravel'); inside the test_welcome_route_returns_success method.
Laravel
Need a hint?

Use assertSee('Welcome to Laravel') to check if the response contains the text.

4
Assert the response JSON has a message key
Add an assertion to check that the response JSON has a key message with value Welcome to Laravel using $response->assertJson(['message' => 'Welcome to Laravel']); inside the test_welcome_route_returns_success method.
Laravel
Need a hint?

Use assertJson(['message' => 'Welcome to Laravel']) to check the JSON response content.