Skip to content

Instantly share code, notes, and snippets.

@luizdamim
Last active May 21, 2019 17:36
Show Gist options
  • Save luizdamim/1a9aaff574a0d4105a31676ef8a8d377 to your computer and use it in GitHub Desktop.
Save luizdamim/1a9aaff574a0d4105a31676ef8a8d377 to your computer and use it in GitHub Desktop.
Elixir manual fixtures
# tests/support/fixtures/accounts_fixtures.ex
defmodule Nononon.TestFixtures.AccountsFixtures do
alias Nononon.Repo
alias Nononon.Accounts.{
Group,
User,
Permission,
GroupUser,
GroupPermission
}
def build(type, attrs) do
type |> build() |> struct(attrs)
end
def insert!(type, attrs \\ []) do
type |> build(attrs) |> Repo.insert!()
end
# Fixtures
def build(:group) do
seq = Enum.random(1..1000)
%Group{
name: "Group #{seq}"
}
end
def build(:user) do
seq = Enum.random(1..1000)
%User{
name: "John Doe #{seq}",
email: "john.doe.#{seq}@gmail.com"
}
end
def build(:permission) do
seq = Enum.random(1..1000)
%Permission{
module: "module #{seq}",
name: "permission #{seq}"
}
end
def build(:group_member) do
group = build(:group)
user = build(:user)
%GroupUser{
group: group,
user: user
}
end
def build(:group_permission) do
group = build(:group)
permission = build(:permission)
%GroupPermission{
group: group,
permission: permission
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment