I'm using ex_machina to create fixture for my unit-tests.
I'm saving some records to the database using ExMachina insert(:comment)
but my assertions are wrong because ExMachina always load relations and it's not the case when I'm fetching with Ecto and not calling Repo.preload
.
Let's say I have 2 factories comment
and user
:
def user_factory do
%User{
name: "some name"
}
end
def comment_factory do
%Comment{
content: "some content",
user: build(:user)
}
end
when I'm testing
test "should retrieve last comment" do
comment = fixture(:comment)
assert Comment.get_last_comment() == comment
end
Assertion if failing because on the left I have
%Comment{
...,
content: "some content",
user: #Ecto.Association.NotLoaded<association :user is not loaded>
}
and on the right
%Comment{
...,
content: "some content",
user: %User{ name: "some name" }
}
I'm trying to avoid:
using
Repo.preload
on my left records because sometimes there are quite a lot of dependenciestesting each property individually
Thanks by advance for your help
I've tackled this problem by defining a
equal_records
function intest/support/data_case.ex
that just compares the type of the structs and the ids of the records:Usage:
To also test lists of records, add these functions too: