-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
53 additions
and
43 deletions.
There are no files selected for viewing
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ defmodule ExMachina do | |
quote do | ||
@before_compile unquote(__MODULE__) | ||
|
||
import ExMachina, only: [sequence: 2] | ||
import ExMachina, only: [sequence: 2, factory: 2] | ||
|
||
def build(factory_name, attrs \\ %{}) do | ||
ExMachina.build(__MODULE__, factory_name, attrs) | ||
|
@@ -69,12 +69,21 @@ defmodule ExMachina do | |
end | ||
end | ||
|
||
defmacro factory(factory_name, do: block) do | ||
quote do | ||
def factory(unquote(factory_name), var!(attrs)) do | ||
!var!(attrs) # Removes unused variable warning if attrs wasn't used | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
unquote(block) | ||
end | ||
end | ||
end | ||
|
||
@doc """ | ||
Create sequences for generating unique values | ||
## Examples | ||
def factory(:user) do | ||
factory :user do | ||
%{ | ||
# Will generate "[email protected]" then "[email protected]", etc. | ||
email: sequence(:email, &"me-\#{&1}@foo.com") | ||
|
@@ -88,7 +97,7 @@ defmodule ExMachina do | |
## Example | ||
def factory(:user) do | ||
factory :user do | ||
%{name: "John Doe", admin: false} | ||
end | ||
|
@@ -137,7 +146,7 @@ defmodule ExMachina do | |
## Example | ||
def factory(:user) do | ||
factory :user do | ||
%{name: "John Doe", admin: false} | ||
end | ||
|
@@ -176,19 +185,10 @@ defmodule ExMachina do | |
|
||
defmacro __before_compile__(_env) do | ||
quote do | ||
@doc """ | ||
Calls factory/1 with the passed in factory name | ||
This allows you to define factories without the `attrs` param. | ||
""" | ||
def factory(factory_name, _attrs) do | ||
__MODULE__.factory(factory_name) | ||
end | ||
|
||
@doc """ | ||
Raises a helpful error if no factory is defined. | ||
""" | ||
def factory(factory_name) do | ||
def factory(factory_name, _) do | ||
raise UndefinedFactory, factory_name | ||
end | ||
|
||
|
@@ -206,7 +206,9 @@ defmodule ExMachina do | |
defmodule MyApp.Factories do | ||
use ExMachina.Ecto, repo: MyApp.Repo | ||
def factory(:user), do: %User{name: "John"} | ||
factory :user do | ||
%User{name: "John"} | ||
end | ||
end | ||
# Will build and save the record to the MyApp.Repo | ||
|
@@ -216,7 +218,9 @@ defmodule ExMachina do | |
# Note, we are not using ExMachina.Ecto | ||
use ExMachina | ||
def factory(:user), do: %User{name: "John"} | ||
factory :user do | ||
%User{name: "John"} | ||
end | ||
def save_function(record) do | ||
# Poison is a library for working with JSON | ||
|
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,15 @@ defmodule ExMachinaTest do | |
defmodule MyApp.Factories do | ||
use ExMachina | ||
|
||
def factory(:user) do | ||
factory :user do | ||
%{ | ||
id: 3, | ||
name: "John Doe", | ||
admin: false | ||
} | ||
end | ||
|
||
def factory(:email) do | ||
factory :email do | ||
%{ | ||
email: sequence(:email, &"me-#{&1}@foo.com") | ||
} | ||
|
@@ -27,18 +27,16 @@ defmodule ExMachinaTest do | |
defmodule MyApp.NoSaveFunction do | ||
use ExMachina | ||
|
||
def factory(:foo), do: %{foo: :bar} | ||
factory(:foo) do | ||
%{foo: :bar} | ||
end | ||
end | ||
|
||
test "sequence/2 sequences a value" do | ||
assert "[email protected]" == MyApp.Factories.build(:email).email | ||
assert "[email protected]" == MyApp.Factories.build(:email).email | ||
end | ||
|
||
test "factories can be defined without the attrs param" do | ||
assert MyApp.Factories.build(:user) == MyApp.Factories.factory(:user) | ||
end | ||
|
||
test "raises a helpful error if the factory is not defined" do | ||
assert_raise ExMachina.UndefinedFactory, "No factory defined for :foo", fn -> | ||
MyApp.Factories.build(:foo) | ||
|
You can do
_ = var!(attrs)
or add a guard, especially if you expect it to be a map.