Reputation: 1387
I usually check if my test returns the expected result like this:
company = company_fixture() # inserts a company in the database with default attributes
assert Profile.get_company!(company.id) == company
But this fails
Assertion with == failed
code: assert Profile.get_company!(company.id) == company
left: %MyApp.Profile.Company{
customers: [],
employees: [],
# more attributes, all matching
}
right: %Databaum.Profile.Company{
customers: #Ecto.Association.NotLoaded<association :customers is not loaded>,
employees: #Ecto.Association.NotLoaded<association :employees is not loaded>,
# more attributes, all matching
}
What is the recommended way of handling that? I want to obviously avoid preloading the associations in the test because that would avoid checking the fact that they are not preloaded in Profile.get_company!/1
.
Upvotes: 0
Views: 1839
Reputation: 447
I am afraid that your assert also fails because you are dealing with different structs. You could simply iterate through your struct and remove the fields with have %Ecto.Association.NotLoaded{}
as value and then also remove those fields from your first struct, then assert both are equal, like this:
def remove_not_loaded_associations(struct_with_assoc, struct_without_assoc) do
keys_to_remove =
struct_without_assoc
|> Map.from_struct()
|> Enum.filter(fn {_k, v} -> match?(%Ecto.Association.NotLoaded{}, v))
|> Keyword.keys()
map1 =
struct_with_assoc
|> Map.from_struct()
|> Map.drop(keys_to_remove)
map2 =
struct_without_assoc
|> Map.from_struct()
|> Map.drop(keys_to_remove)
{map1, map2}
end
# ...
{map1, map2} = remove_not_loaded_associations(company, Profile.get_company!(company.id))
assert map1 == map2
Upvotes: 2