Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active July 13, 2016 16:39
Show Gist options
  • Save paulcsmith/3be60ca84bea1d09a71e1f970a628162 to your computer and use it in GitHub Desktop.
Save paulcsmith/3be60ca84bea1d09a71e1f970a628162 to your computer and use it in GitHub Desktop.
ExMachina 2.0?
defmodule MyApp.Factory do
def user_factory do
%User{
email: "[email protected]",
name: "Paul"
}
end
end
# Makes it easy to organize factories however you want
import MyApp.Factory
# Could also alias factories easily this way
alias MyApp.UserFactory # and do UserFactory.new |> build
# Imports insert, insert_list, build, build_list, etc.
use ExMachina.Ecto, repo: MyApp.Repo
# Could potentially import other strategies json_encode, json_encode_list
use MyApp.JsonEncodeStrategy
user = user_factory |> build(email: "whatever")
users = user_factory |> build_list
# Old way is: admins = insert_list(:user, admin: true) Slightly shorter, but less flexible
admins = insert_list(:user, admin: true)
admins = user_factory |> insert_list(admin: true)
# Alternatively, you could alias Factory and use different function names like this:
defmodule MyApp.Factory do
def user do
%User{
email: "[email protected]",
name: "Paul"
}
end
end
alias MyApp.Factory
Factory.user |> insert_list(admin: true)
@paulcsmith
Copy link
Author

This might be cool because it could be a lot more flexible, maybe easier to understand (have to try it out with people), and could remove some macros. Might be worth trying on some internal projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment