Naar de inhoud
Recognized by Laravel Match je project Contact

Volg best practices voor testen

Voor het laatst bijgewerkt op:

Beschrijving

Gebruik de test-helpers en -patronen van Laravel die suites sneller, duidelijker en minder foutgevoelig maken.

Aanbevolen situatie

Gebruik dit bij het schrijven of onderhouden van tests in een Laravel-applicatie.

Menselijke begeleiding

Naast het kiezen van een testframework (zie Gebruik PHPUnit of Pest) zijn er verschillende Laravel-specifieke testpatronen die de snelheid, betrouwbaarheid en expressiviteit van tests verbeteren. Denk aan het gebruik van LazilyRefreshDatabase voor performance, model-assertions voor duidelijkheid, factory states voor leesbaarheid, en het correct ordenen van fakes om te voorkomen dat model events breken.

Waarom

  • Snellere testsuites: LazilyRefreshDatabase draait migrations alleen wanneer het schema verandert, wat grote suites aanzienlijk versnelt
  • Expressieve assertions: assertModelExists() is type-veiliger en levert duidelijkere foutmeldingen op dan een kale assertDatabaseHas()
  • Zelfdocumenterende tests: Benoemde factory states zoals ->unverified() communiceren de bedoeling beter dan handmatige attribuut-overrides
  • Correct gedrag: Event::fake() na de factory-setup aanroepen voorkomt dat model events die factories nodig hebben stilzwijgend breken

Geschikt voor

  • Alle Laravel-applicaties met testsuites
  • Teams die hun CI/CD-pipelines willen versnellen
  • Projecten met complexe factory-relaties
  • Applicaties met event-gedreven of queue-gebaseerd gedrag dat getest moet worden

Minder geschikt voor

  • Zeer kleine testsuites waar snelheidsoptimalisaties niet merkbaar zijn
  • Projecten die de ingebouwde testfuncties van Laravel niet gebruiken

Voorbeelden

Gebruik LazilyRefreshDatabase

// Slower: runs all migrations every test run
use Illuminate\Foundation\Testing\RefreshDatabase;

// Faster: only migrates when schema changes
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;

Gebruik model-assertions

// Bad: raw database assertion
$this->assertDatabaseHas('users', ['id' => $user->id]);

// Good: more expressive and type-safe
$this->assertModelExists($user);

Gebruik factory states en sequences

Benoemde states maken tests zelfdocumenterend:

// Bad: manual attribute override
User::factory()->create(['email_verified_at' => null]);

// Good: named state communicates intent
User::factory()->unverified()->create();

Roep Event::fake() aan na de factory-setup

Model-factories vertrouwen op model events (bijv. creating om UUID's te genereren). Als je Event::fake() vóór de factory-aanroepen plaatst, worden die events onderdrukt en ontstaan er kapotte modellen:

// Bad: Event::fake() prevents factory model events
Event::fake();
$user = User::factory()->create();

// Good: create models first, then fake events
$user = User::factory()->create();
Event::fake();

Gebruik Exceptions::fake() voor exception-assertions

Gebruik in plaats van withoutExceptionHandling() liever Exceptions::fake() om te controleren dat de juiste exception werd gerapporteerd terwijl het request normaal wordt voltooid.

Gebruik recycle() om relatie-instanties te delen

Zonder recycle() maken geneste factories afzonderlijke instanties van dezelfde conceptuele entiteit:

Ticket::factory()
    ->recycle(Airline::factory()->create())
    ->create();

Meer info

Boost guideline

---
title: Follow Testing Best Practices
description: Use Laravel’s testing helpers and patterns that make suites faster, clearer, and less error-prone.
recommended_situation: Use when writing or maintaining tests in a Laravel application.
---

- Prefer Laravel-native testing helpers that improve speed and clarity, including `LazilyRefreshDatabase`, model assertions, and named factory states.
- Structure test setup so fakes do not accidentally disable model events or other behavior required to build valid test data.
- Use expressive assertions and realistic factories to verify behavior instead of low-level or incidental implementation details.
- Reach for Laravel testing utilities such as `Exceptions::fake()` and `recycle()` when they make intent or correctness clearer.
Skill

Laravel Boost Skill

Gebruik deze skill om de richtlijn rechtstreeks toe te passen met een AI-assistent.

Use this skill when a Laravel task touches this best practice. It is self-contained so it can be installed independently by Laravel Boost or another agent-skill system.

## Core Guidance



## Why It Matters

- Apply the best practice consistently and keep the implementation focused.

## Apply When

- Laravel work that directly overlaps with this practice.

## Be Careful When

- Tasks outside this practice; use a more specific skill instead.

## Canonical Source

- Full best practice: https://github.com/Dutch-Laravel-Foundation/best-practices/blob/main/testing/follow-testing-best-practices/BEST_PRACTICE.md
- Dutch translation: https://github.com/Dutch-Laravel-Foundation/best-practices/blob/main/testing/follow-testing-best-practices/translations/nl.md

## Workflow

1. Inspect the user's Laravel code before recommending changes.
2. Identify the narrow rule from this best practice that applies to the task.
3. Prefer Laravel's built-in conventions and documented APIs over custom abstractions.
4. Keep examples focused on this practice; reference other skills or practices when the task crosses boundaries.
5. Verify code changes with the project's available tests, linters, static analysis, or framework checks.

## Review Checklist

- The recommendation is Laravel-specific and grounded in this practice.
- Code examples use realistic Laravel file names, class names, and method names.
- The advice avoids mixing unrelated architecture, deployment, security, or testing topics.
- Related practices are mentioned when useful, but not re-explained in full.
- Dutch output, when requested, keeps framework and API names intact.