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

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

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)
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)
sequence(name)

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
sequence(name, formatter)

Create sequences for generating unique values

Examples

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