Skip to content

Commit

Permalink
Document test factory variant best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hobson committed Apr 20, 2023
1 parent 6961b61 commit 5799a21
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions docs/backend/testing/factory-test-data-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ By packaging them in nice identical containers, we get some useful features that

* **Use traits for commonly used patterns.**

Don't create variants of the Build functions. Where possible, create a `Trait` - they are much more flexible and allow us to mix and match useful traits. See [Traits](#traits).
Don't create variants of the Build functions unless the default
build function should build something that another variant would
like to exclude. An example is `BuildDutyLocation` and
`BuildDutyLocationWithoutTransportationOffice`

Where possible, create a `Trait` - they are much more flexible and
allow us to mix and match useful traits. See [Traits](#traits).

* **Don't reuse your customization list.**

The Build functions modify the customization list that is passed in. Create a trait if you want to reuse customizations.

* **Select the right `Type` for your `Customization`**.

See [CustomTypes](#customtypes).

* **Use LinkOnly to hook up a premade model.**

See [LinkOnly](#linkonly)

* **If a `pkg/factory/<model>_factory.go` exists for your model, please use it.**
Expand Down Expand Up @@ -132,7 +138,7 @@ To create an `AdminUser` whose `User` should have a `customEmail`, just provide
user := BuildAdminUser(suite.DB(), []Customization{
{
Model: models.User{ // ← Type field not required, defaults to model type
LoginGovEmail: customEmail,
LoginGovEmail: customEmail,
},
},
}, nil)
Expand Down

0 comments on commit 5799a21

Please sign in to comment.