Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active October 12, 2015 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcsmith/93bfecf15caffb9a209e to your computer and use it in GitHub Desktop.
Save paulcsmith/93bfecf15caffb9a209e to your computer and use it in GitHub Desktop.
Defining factories in ExMachina. Some ideas
# This is a bit more flexible. Worried people will try to call it directly :S
# Also throws warnings if you put functions in between the function definitions
def factory(:user, attrs) do
%User{
first_name: "Paul" ,
last_name: "Smith"
email: put_new_email(attrs)
}
end
factory :user do
%User{
first_name: "Paul",
last_name: "Smith"
}
end
def build(:user, attrs \\ []) do
do_build(:user, put_new_email(attrs))
end
def create(:user, attrs \\ []) do
do_create(:user, attrs) |> add_settings
end
defp put_new_email(attrs) do
Map.put_new(attrs, :email, attrs.first_name <> "@foo.com")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment