Medium15 minLaravel Fundamentals
UpdatedAug 5, 2026
Edit

Laravel Testing

Question Variations

  • "When should a Laravel test be a feature test?"
  • "Why use factories instead of fixture files?"
  • "How do you fake a queue or event in a test?"

Why This Is Asked

Laravel testing tools assess whether candidates choose the appropriate scope and assert user-visible application behavior.

Key Concepts

  • Unit tests do not boot the Laravel application.
  • Feature tests exercise routes, middleware, and framework services.
  • Factories create controlled model state.
  • Tests should isolate database and external side effects.

Question Variations

  • “When should a Laravel test be a feature test?”
  • “Why use factories instead of fixture files?”
  • “How do you fake a queue or event in a test?”

Answers by Technology

+ Add Variant
LaravelImprove this answer ✏️

Expected Answer (Laravel 13 / PHP 8.3+)

Use unit tests for isolated PHP logic and feature tests for observable Laravel behavior such as routes, middleware, validation, policies, and database interactions. Factories create focused model state, while fakes keep queues, events, mail, and external side effects deterministic. Assert HTTP status and body contracts, not merely that a controller was called.

Why It Matters

Feature tests catch framework wiring problems while isolated tests keep feedback fast.

Code Example

test('creates a user', function () {
    $this->postJson('/api/users', ['email' => 'ada@example.test'])
        ->assertCreated()
        ->assertJsonPath('email', 'ada@example.test');
});

Common Mistakes

  • Using a full feature test for pure logic: The suite becomes unnecessarily slow.
  • Allowing real queues or mail in tests: Side effects make tests flaky and unsafe.

Follow-up Questions

  • What does Queue::fake() provide? (Answer: It records dispatches without running workers.)
  • When use a feature test? (Answer: When verifying Laravel HTTP or framework integration behavior.)