Laravel interview question · Most asked

How do you test a Laravel application? Feature tests, unit tests and fakes.

Summary

Feature tests send HTTP requests through the whole framework ($this->postJson('/api/orders', ...)->assertCreated()); unit tests cover isolated classes. Laravel's fakes (Queue::fake(), Mail::fake(), Http::fake(), Event::fake(), Storage::fake()) replace side effects so tests are fast and deterministic.

Longer Answer

  • Use RefreshDatabase (wraps each test in a transaction, or migrates once) with a real database engine where possible; SQLite in memory is fast but behaves differently from MySQL or PostgreSQL (JSON, locking, strictness).
  • Factories create the exact records a test needs.
  • actingAs($user) authenticates; assert authorization by calling endpoints as the wrong user and expecting 403/404.
  • Http::fake([...]) plus Http::preventStrayRequests() ensure no test calls a real external API.
  • Queue::fake() then Queue::assertPushed(SendInvoice::class, fn ($job) => ...).
  • Time: $this->travel(5)->minutes() or Carbon::setTestNow().

Most value comes from feature tests of real endpoints; unit-test complex domain logic separately.

Key Points

  • Feature tests through HTTP; unit tests for logic.
  • Fakes for queue, mail, HTTP, events, storage.
  • RefreshDatabase; beware SQLite differences.
  • preventStrayRequests().

Explanation

What interviewers look for

A pragmatic testing strategy that includes authorization tests and isolates external services.

Follow-up questions

  • How do you test a queued job's behaviour, not just that it was dispatched?

More most-asked Laravel questions

← All Laravel questions