View Source Refactory (refactory v0.1.1)

Refactory allows generating Ecto records with nested overrides for your tests.

Factory module

To start using Refactory, first define a factory module:

defmodule MyApp.Factory do
  use Refactory, repo: MyApp.Repo
end

Usage

The factory module has two functions:

  • build/2 generates an Ecto record with the given traits applied
  • create/2 inserts an Ecto record into the database

Traits

A trait can be

  • a Map in which each key-value pair is either
    • a field with its value
    • an association with a trait (for belongs_to, has_one, and embeds_one)
    • soon: an association with a list of traits (for has_many and embeds_many)
  • a custom trait defined in the factory module (see below)
  • a Tuple with multiple traits to be applied

Basic example

defmodule MyApp.Factory do
  use Refactory, repo: MyApp.Repo
end

MyApp.Factory.build(MyApp.List, %{
  title: "Refined List",
  created_by_user: %{email: "test@email.org"}
})

%MyApp.List{
  title: "Refined List",
  created_by_user: %MyApp.User{
    email: "test@email.org"
  }
}

Default traits

Default traits can be defined in the factory module. They are always applied first.

defmodule MyApp.Factory do
  use Refactory, repo: MyApp.Repo

  def trait(MyApp.List, :default) do
    %{
      title: "Default Title"
    }
  end
end


MyApp.Factory.build(MyApp.List)

%MyApp.List{title: "Default Title"}

Custom traits

Custom traits can be defined in the factory module and then used by their name.

defmodule MyApp.Factory do
  use Refactory, repo: MyApp.Repo

  def trait(MyApp.List, :default) do
    %{
      title: "Default Title"
    }
  end

  def trait(MyApp.List, :with_admin_user) do
    %{
      created_by_user: %{
        role: :admin
      }
    }
  end
end


MyApp.Factory.build(MyApp.List, :with_admin_user)

%MyApp.List{title: "Default Title", created_by_user: %MyApp.User{role: :admin}}

Summary

Functions

Generates an Ecto record with the given traits applied

Inserts an Ecto record with the given traits applied into the database

Functions

Link to this function

build(module, type, traits \\ %{})

View Source

Generates an Ecto record with the given traits applied

Link to this function

create(module, type, traits \\ %{})

View Source

Inserts an Ecto record with the given traits applied into the database