ExMachina

Defines functions for generating data

In depth examples are in the README

Summary

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

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

Create sequences for generating unique values

Callback implementation for c::application.start/2

Functions

build(module, factory_name, attrs \\ %{})

Builds a factory with the passed in factory_name and attrs

Example

def factory(:user, _attrs) do
  %{name: "John Doe", admin: false}
end

# Returns %{name: "John Doe", admin: true}
build(:user, admin: true)
build_list(module, number_of_factories, factory_name, attrs \\ %{})

Builds and returns X records with the passed in factory_name and attrs

Example

# Returns a list of 3 users
build_list(3, :user)
build_pair(module, factory_name, attrs \\ %{})

Builds and returns 2 records with the passed in factory_name and attrs

Example

# Returns a list of 2 users
build_pair(:user)
create(module, built_record)

Builds and saves a factory with the passed in factory_name

If using ExMachina.Ecto it will use the Ecto Repo passed in to save the record automatically.

If you are not using ExMachina.Ecto, you need to define a save_record/1 function in your module. See save_record docs for more information.

Example

def factory(:user, _attrs) do
  %{name: "John Doe", admin: false}
end

# Saves and returns %{name: "John Doe", admin: true}
create(:user, admin: true)
create(module, factory_name, attrs \\ %{})
create_list(module, number_of_factories, factory_name, attrs \\ %{})

Creates and returns X records with the passed in factory_name and attrs

Example

# Returns a list of 3 saved users
create_list(3, :user)
create_pair(module, factory_name, attrs \\ %{})

Creates and returns 2 records with the passed in factory_name and attrs

Example

# Returns a list of 2 saved users
create_pair(:user)
sequence(name, formatter)

Create sequences for generating unique values

Examples

def factory(:user, _attrs) do
  %{
    # Will generate "me-0@example.com" then "me-1@example.com", etc.
    email: sequence(:email, &"me-#{&1}@foo.com")
  }
end
start(type, args)

Callback implementation for c::application.start/2.

Macros

factory(factory_name, list)