Feature Testing with HTTP Requests
Feature tests exercise a request the way a real user or client would — hitting a route, passing through middleware, and checking the resulting response.
Generating a Feature Test
php artisan make:test PostControllerTest
Making Requests in Tests
public function test_guest_cannot_create_a_post()
{
$response = $this->post('/posts', ['title' => 'Hello']);
$response->assertRedirect('/login');
}
public function test_authenticated_user_can_create_a_post()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/posts', [
'title' => 'My First Post',
]);
$response->assertStatus(201);
$this->assertDatabaseHas('posts', ['title' => 'My First Post']);
}
Useful Assertions
- assertStatus(), assertOk(), assertRedirect()
- assertJson(), assertJsonStructure() for API responses
- assertDatabaseHas(), assertDatabaseMissing() for database state
Feature tests confirm the whole request works end to end. But real applications often call external services or complex dependencies that shouldn't run during a test — which is where mocking and stubbing help.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
