ex_machina v2.0.0 ExMachina
Defines functions for generating data
In depth examples are in the README
Summary
Functions
Builds a factory with the passed in factory_name and attrs
Builds and returns X records with the passed in factory_name and attrs
Builds and returns 2 records with the passed in factory_name and attrs
Shortcut for creating unique string values. Similar to sequence/2
Create sequences for generating unique values
Functions
Builds a factory with the passed in factory_name and attrs
Example
def user_factory do
%{name: "John Doe", admin: false}
end
# Returns %{name: "John Doe", admin: true}
build(:user, admin: true)
Builds and returns X records with the passed in factory_name and attrs
Example
# Returns a list of 3 users
build_list(3, :user)
Builds and returns 2 records with the passed in factory_name and attrs
Example
# Returns a list of 2 users
build_pair(:user)
Shortcut for creating unique string values. Similar to sequence/2
If you need to customize the returned string, see ExMachina.sequence/2
.
Note that sequences keep growing and are not reset by ExMachina. Most of the
time you won’t need to reset the sequence, but when you do need to reset them,
you can use ExMachina.Sequence.reset/0
.
Examples
def user_factory do
%User{
# Will generate "username0" then "username1", etc.
username: sequence("username")
}
end
def article_factory do
%Article{
# Will generate "Article Title0" then "Article Title1", etc.
title: sequence("Article Title")
}
end