How do I pass tests that fail due to unloaded associations?

I wrote the following test.

test "list_users/1 returns all users in a tenant", %{tenant: tenant} do
  user = insert(:user, tenant: tenant)
  user_2 = insert(:user, tenant: tenant)

  assert Accounts.list_users(tenant) == [user_2, user]
end

      

I am testing to see if I get all users back from the tenant, in order to do this I need to assign my plants to the tenant I want to test with. The problem is when I assign a tenant to the user, the association is now loaded. The returned users are not, hence the test failed with the following difference.

tenant: #Ecto.Association.NotLoaded<association :tenant is not loaded>

      

against

tenant: %MyApp.Accounts.Tenant{__meta__: #Ecto.Schema.Metadata<:loaded, "tenants">, domain: "pharma-13", id: 484, inserted_at: ~N[2017-06-14 15:10:42.125243], logo: "some_logo_path.png", name: "Pharma", updated_at: ~N[2017-06-14 15:10:42.125250]

      

Do I have to unload the associations somehow? I certainly don't want to add preloading to my function, just to pass the test.

+3


source to share





All Articles