ExZample v0.7.0 ExZample.DSL View Source
Defines a domain-speficic language(DSL) to simplify the creation of your factories.
You can use this DSL by using by defining a module and adding the use
directive. For example:
defmodule MyApp.Factories do
use ExZample.DSL
alias MyApp.User
factory :user do
example do
%User{
id: sequence(:user_id),
first_name: "Abili"
last_name: "de bob"
}
end
end
sequence :user_id
end
It will generate the modules, functions and the aliases manifest to be loaded
when the ex_zample
app starts. Then, to use your factories, don't forget to
start your app. For example:
# in your test_helper.exs
:ok = Application.ensure_started(:ex_zample)
This way, all factorites your defined using the ExZample.DSL
will be
loaded module.
Options
You can pass the following options to the use
directive:
scope
(default::global
), the:scope
that all aliases of factories will be stored
Link to this section Summary
Functions
Defines a module factory with helpers imported by default.
Defines a sequence function with a optional scope
, mandatory name
and an
optional anonymus function in return
as value transformation.
Defines a sequence function with a given alias name
and a anonymus function
on return
as value transformation.
Link to this section Types
name_or_scoped_name()
View Sourcename_or_scoped_name() :: name :: atom() | [scoped_name()]
Link to this section Functions
factory(name_or_scoped_name, list)
View Source (macro) (since 0.6.0)factory(name_or_scoped_name(), [{:do, Macro.t()}]) :: Macro.t()
Defines a module factory with helpers imported by default.
If you pass an atom
for factory name, such as user
, the module will be
generated with UserFactory
. If you pass a scope, like: my_app: :user
, the
module name will become MyAppUserFactory
.
The factory body has all functions from ExZample
imported. It also has access
to example
DSL helper that generates a function definition with behaviour
annotations. Taking those helpers out, everything else work as normal Elixir
module.
Defines a sequence function with a optional scope
, mandatory name
and an
optional anonymus function in return
as value transformation.
Examples
sequence(:user_id)
sequence(scoped: :user_id, return: &"user_#{&1}")
# later you can invoke like this:
ExZample.sequence(:user_id)
1
ExZample.ex_zample(ex_zample_scope: :scoped)
ExZample.sequence(:user_id)
"user_1"
A function will be generated in the current module following the pattern:
{scope}_{sequence_name}_sequence
.
sequence(name, list)
View Source (macro) (since 0.7.0)sequence(name :: atom(), [{:return, ExZample.sequence_fun()}]) :: Macro.t()
Defines a sequence function with a given alias name
and a anonymus function
on return
as value transformation.
Examples
sequence(:user_id, return: &("user_#{&1}")
# later you can invoke like this:
ExZample.sequence(:user_id)
"user_1"
A function will be generated in the current module following the pattern:
{scope}_{sequence_name}_sequence
.