Factories & Building
Last updated
Last updated
First we'll learn how to create factories and how to use them to build entities. Let's say our application has users that we want to create for use in our tests, and each user has:
a name
an email
a role - one of 'admin' or 'user'
We might write our factory definition as:
To create a factory we need an id
and a factory definition; a map with at least a template
key:
The id is a keyword that uniquely identifies the factory, namespaced keywords are recommended. Fabrikk accepts factory ids and instances interchangeably in most places (see for more)
The template details the keys that our entity will have, and the values of those keys
We'll explore more options in the course of this tutorial, and you can take a look at to see a comprehensive list.
Now that we have a factory we can get building:
But wait, this generates the same user every time! We can pass build options to build
to vary the output, using the with
option: