Params (including associations)
Every example needs params.
Params are described with the params
function. It's typically first in an example's description, but it can be anywhere.
`params_like`
You can also use param_like
to refer to an example previously described. Given the definition of :bossie
above, the following two are equivalent:
Note: does params_like work if bossie used an `id-of`?
The :except
can be omitted, in which case :jake
would have the same params as :bossie
.
The :except
clause can add fields that aren't present in the original:
Ecto associations
Associations are handled by giving specific values to foreign keys. Consider an Animal
schema with a belongs_to
association to a Species
schema. That's represented by giving a value to the Animal
's species_id
foreign key:
Most likely, you don't want to give :species_id
an explicit integer or GUID value. Instead, you want the database to create the Species
row and use whatever value it gave as the primary key. That is accomplished as follows. Suppose you have a SpeciesExamples
module containing a :bovine
example:
Then :bossie
can be created "within" species :bovine
like this:
At the moment, you can only fetch the primary key of another example, and that primary key must be named :id
. All that's easily changed upon request.
The previous example showed how to refer to an example in a different module. If you want to use an example in the same module, you can abbreviate to just the example name:
Mechanism
Whenever an example is used, the very first step is to insert any other examples it depends on. Then those values are used to construct the examples params. If you trace the running of an example, you'll see those recursive creations. Here's an example of creating `:bossie
Last updated
Was this helpful?