# 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"}]

Add `:ex_machina` to your application list:

def application do
  [applications: app_list(Mix.env)]

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: ""}

  def factory(:article) do
      title: "My Awesome Article"

  def factory(:comment, opts) do
      body: "This is great!",
      article_id: assoc(opts, :article).id

  def create_record(map) do
    # This example uses Ecto to save records

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,

    assert html_response(conn, 200) =~ article.title
    assert html_response(conn, 200) =~ comment.body

## License

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

## About 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][community] or
[hire us][hire] to design, develop, and grow your product.


## Inspiration

* [Fixtures for Ecto](
* [Factory Girl](