ExMachina

ExMachina makes it easy to create test data and associations. It works great with Ecto, but is configurable to work with any persistence library.

Installation

In mix.exs, add the ExMachina dependency:

def deps do
[{:ex_machina, "~> 0.0.1"}]
end

Add :ex_machina to your application list:

def application do
[applications: app_list(Mix.env)]
end
defp app_list(:test), do: [:ex_machina | app_list]
defp app_list(_), do: app_list
defp app_list, do: [:logger]

Examples

# test/factories.ex
defmodule MyApp.Factories do
use ExMachina
def factory(:config) do
# Factories can be plain maps
%{url: "http://example.com"}
end
def factory(:article) do
%Article{
title: "My Awesome Article"
}
end
def factory(:comment, opts) do
%Comment{
body: "This is great!",
article_id: assoc(opts, :article).id
}
end
def create_record(map) do
# This example uses Ecto to save records
MyApp.Repo.insert!(map)
end
end

Then use it in your tests. This is an example with Phoenix.

defmodule MyApp.MyModuleTest do
use MyApp.ConnCase
# You can also import this in your MyApp.ConnCase if using Phoenix
import MyApp.Factories
test "shows comments for an article" do
conn = conn()
article = create(:article)
comment = create(:comment, article: article)
conn = get conn, article_path(conn, :show, article.id)
assert html_response(conn, 200) =~ article.title
assert html_response(conn, 200) =~ comment.body
end
end

License

ExMachina is Copyright © 2015 thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About thoughtbot

thoughtbot

ExMachina is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open source software! See our other projects or hire us to design, develop, and grow your product.

Inspiration