Feature Tests
Feature tests send HTTP requests through the full stack and assert on the JSON response, database state, and session.
Feature tests send HTTP requests through the full stack and assert on the JSON response, database state, and session.
class PostTest extends TestCase {
use RefreshDatabase;
public function test_authenticated_user_can_create_post(): void {
$user = User::factory()->create();
$this->actingAs($user)
->postJson("/api/posts", ["title" => "Hello", "body" => "World"])
->assertCreated()
->assertJsonPath("data.title", "Hello");
$this->assertDatabaseHas("posts", ["title" => "Hello", "user_id" => $user->id]);
}
}
Feature tests give the highest confidence because they test the full request cycle without a browser.