Feature Tests

Feature Tests for HTTP Endpoints

A feature test sends a request through the whole kernel without opening a socket. get() and post() act like a browser form. getJson() and postJson() add Accept: application/json, so validation fails with 422. actingAs($user) logs a user in, CSRF checks are skipped, and withoutExceptionHandling() makes an unexplained 500 throw its real exception:

tests/Feature/ProductTest.php (excerpt)PHP
public function test_the_catalog_lists_products_by_name(): void
{
    Product::factory()->create(['name' => 'SQL Basics', 'price' => 19.5]);
    Product::factory()->create(['name' => 'Apache Cookbook']);
    $this->getJson('/products')
        ->assertOk()
        ->assertJsonPath('count', 2)
        ->assertJsonPath('data.0.name', 'Apache Cookbook')
        ->assertJsonPath('data.1.price', 19.5);
}
Output
   FAIL  Tests\Feature\ProductTest
  ⨯ the catalog lists products by name                                   0.01s
...
  Failed asserting that '19.50' is identical to 19.5.
...
  Tests:    1 failed, 6 passed (30 assertions)