Last active
October 12, 2015 22:42
-
-
Save paulcsmith/93bfecf15caffb9a209e to your computer and use it in GitHub Desktop.
Defining factories in ExMachina. Some ideas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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