Last active
July 13, 2016 16:39
-
-
Save paulcsmith/3be60ca84bea1d09a71e1f970a628162 to your computer and use it in GitHub Desktop.
ExMachina 2.0?
This file contains hidden or 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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