Why this lesson matters

Understand database tests and apply it in a small Laravel feature. Laravel tests are executable contracts for behavior. Feature tests cross the HTTP boundary, database tests verify persistence effects, and factories give repeatable setup.

How to reason about it

  • For Database Tests, the outcome to verify is: Test behavior through application boundaries, not only implementation details.
  • In Database Tests, keep this failure controlled: Testing only framework methods or private implementation details produces confidence that disappears when the real request path changes.
  • Database Tests practice target: Write a feature test that creates data, calls the real endpoint and asserts both the response and resulting database state.

Practical walkthrough

In the Database Tests walkthrough: Test behavior through application boundaries, not only implementation details.

tests/Feature/UserListTest.phpphp
test('authenticated user can list users', function () {
    $user = User::factory()->create();
    $this->actingAs($user)->getJson('/api/users')->assertOk();
});

Practice it yourself

Practice

Database Tests exercise

Write a feature test that creates data, calls the real endpoint and asserts both the response and resulting database state.

  • Record the expected result before execution
  • Test one valid path and one lesson-specific failure path
  • Explain in two lines which boundary owns the decision

Summary