ExMachina
Defines functions for generating data
In depth examples are in the README
Summary
Gets a factory from the passed in attrs, or creates if none is present
Builds a factory with the passed in factory_name
Builds and saves a factory with the passed in factory_name
Creates and returns X records with the passed in factory_name and attrs
Creates and returns 2 records with the passed in factory_name and attrs
Builds a factory with the passed in factory_name and returns its fields
Saves a record when create
is called. Uses Ecto if the repo
option is set
Create sequences for generating unique values
Callback implementation for c::application.start/2
Functions
Gets a factory from the passed in attrs, or creates if none is present
Examples
attrs = %{user: %{name: "Someone"}}
# Returns attrs.user
assoc(attrs, :user)
attrs = %{}
# Creates and returns new instance based on :user factory
assoc(attrs, :user)
attrs = %{}
# Creates and returns new instance based on :user factory
assoc(attrs, :author, factory: :user)
Builds a factory with the passed in factory_name
Example
def factory(:user) do
%{name: "John Doe", admin: false}
end
# Returns %{name: "John Doe", admin: true}
build(:user, admin: true)
Builds and saves a factory with the passed in factory_name
If you pass in repo when using ExMachina it will use the Ecto Repo to save the
record automatically. If you do not pass the repo, you need to define a
save_record/1
function in your module. See save_record
docs for more
information.
Example
def factory(:user) do
%{name: "John Doe", admin: false}
end
# Saves and returns %{name: "John Doe", admin: true}
create(:user, admin: true)
Creates and returns X records with the passed in factory_name and attrs
Example
# Returns a list of 3 users
create_pair(3, :user)
Creates and returns 2 records with the passed in factory_name and attrs
Example
# Returns a list of 2 users
create_pair(:user)
Builds a factory with the passed in factory_name and returns its fields
This is only for use with Ecto models.
Will return a map with the fields and virtual fields, but without the Ecto metadata and associations.
Example
def factory(:user) do
%MyApp.User{name: "John Doe", admin: false}
end
# Returns %{name: "John Doe", admin: true}
fields_for(:user, admin: true)
Saves a record when create
is called. Uses Ecto if the repo
option is set
If you include the repo
option (use ExMachina, repo: MyApp.Repo
) this
function will call insert!
on the passed in repo.
If you do not pass in the repo
option, you must define a custom
save_function/1 for saving the record.
Examples
defmodule MyApp.Factories do
use ExMachina, repo: MyApp.Repo
def factory(:user), do: %User{name: "John"}
end
# Will build and save the record to the MyApp.Repo
MyApp.Factories.create(:user)
defmodule MyApp.JsonFactories do
# Note `repo` was not passed as an option
use ExMachina
def factory(:user), do: %User{name: "John"}
def save_function(record) do
# Poison is a library for working with JSON
Poison.encode!(record)
end
end
# Will build and then return a JSON encoded version of the map
MyApp.JsonFactories.create(:user)
Create sequences for generating unique values
Examples
def factory(:user) do
%{
# Will generate "me-0@example.com" then "me-1@example.com", etc.
email: sequence(:email, &"me-#{&1}@foo.com")
}
end