EctoFixtures

Fixtures for Ecto

Build Status

Usage

Fixture files should be stored in test/fixtures/. The format of a fixture file is as:

# test/fixtures/accounts.exs

accounts model: Account, repo: Repo do
  test do
    email "test@example.com"
    name "Brian Cardarella"
  end
end

In your test file you can access the fixture sets with the EctoFixtures.fixture/1 function:

defmodule MyTestCase do
  use ExUnit.Case
  import EctoFixtures, only: [fixtures: 1]

  test "data test" do
    %{accounts: accounts} = fixtures(:accounts)

    assert accounts.test.email == "test@example.com"
  end
end

This data is also inserted into the database, the resulting data set returned from fixtures/1 is actually an Ecto.Model.

defmodule MyTestCase do
  use ExUnit.Case
  import EctoFixtures, only: [fixtures: 1]

  test "database data is inserted and equal to data set" do
    %{accounts: accounts} = fixtures(:accounts)

    assert accounts.test == Repo.get(Account, accounts.test.id)
  end
end

Associations

Associations can be made between data sets, reference the data set's name and label:

Belongs To

accounts model: Account, repo: Repo do
  brian do
    name "Brian"
  end
end

events model: Event, repo: Repo do
  one do
    name "First Event"
    account accounts.brian
  end
end

Has One

accounts model: Account, repo: Repo do
  brian do
    name "Brian"
    pet pets.boomer
  end
end

pets model: Pet, repo: Repo do
  boomer do
    name "Boomer"
  end
end

Has Many

accounts model: Account, repo: Repo do
  brian do
    name "Brian"
    events [events.one, events.two]
  end
end

events model: Event, repo: Repo do
  one do
    name "First Event"
  end
  two do
    name "Second Event"
  end
end

Authors

We are very thankful for the many contributors

Versioning

This library follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this library. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard, Inc. © 2015

@dockyard

Licensed under the MIT license