Skip to content

Commit

Permalink
Use a real struct (not named struct) in ExMachinaTest
Browse files Browse the repository at this point in the history
We used to have a struct factory defined as `%{__struct__: FooBar}` but
it was confusing since the factory name was `struct_factory`.

So we change to be `foo_bar_factory` and create a module with a struct
called `FooBar`. This has the benefit of allowing us to assert
`%FooBar{}` in tests -- something we couldn't do with the previous
format.
  • Loading branch information
germsvel committed Dec 24, 2020
1 parent 2f1dffd commit bdcb224
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions test/ex_machina_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule ExMachinaTest do
use ExUnit.Case

defmodule FooBar do
defstruct [:name]
end

defmodule Factory do
use ExMachina

Expand All @@ -24,10 +28,8 @@ defmodule ExMachinaTest do
}
end

def struct_factory do
%{
__struct__: Foo.Bar
}
def foo_bar_factory do
%FooBar{}
end

def comment_factory(attrs) do
Expand Down Expand Up @@ -94,7 +96,7 @@ defmodule ExMachinaTest do

test "build/2 raises if passing invalid keys to a struct factory" do
assert_raise KeyError, fn ->
Factory.build(:struct, doesnt_exist: true)
Factory.build(:foo_bar, doesnt_exist: true)
end
end

Expand Down

0 comments on commit bdcb224

Please sign in to comment.