所以我是长生不老药,但我找不到有关此的信息,我真的不明白问题所在。
让我们说:
%Restaurant{} has many %Discount{}
%Discount{} has many %PromoCode{}
工厂:
def restaurant_factory do
%Restaurant{
discounts: [
build(:discount)
]
}
end
def discount_factory do
%Discount{
promo_codes: [
build(:promo_code)
]
}
end
def promo_code_factory do
%PromoCode{}
end
但是当我跑步时:
test "" do
r = insert(:restaurant)
end
我得到: **(ArgumentError)预期变更集数据为%Discount结构,得到:%PromoCode
所以我不能拥有“嵌套”版本?还是其他地方有问题?
实现我想要的东西的正确方法是什么: 创建餐厅,创建与餐厅相关的折扣并创建与折扣相关的促销代码?
完整错误:
1) test load discount by promo code (Restaurants.Domain.LoadDiscountsServiceTest)
test/services/load_discounts/service_test.exs:42
** (ArgumentError) expected changeset data to be a Elixir.Restaurants.Domain.Models.Discount struct, got: %Res
taurants.Domain.Models.PromoCode{__meta__: #Ecto.Schema.Metadata<:built, "promo_codes">, code: "XXYY6Z", created_at
: nil, discounts: #Ecto.Association.NotLoaded<association :discounts is not loaded>, discounts_id: nil, id: nil, up
dated_at: nil, use_limit: 3}
stacktrace:
(ecto) lib/ecto/changeset/relation.ex:190: Ecto.Changeset.Relation.assert_changeset_struct!/2
(ecto) lib/ecto/changeset/relation.ex:148: Ecto.Changeset.Relation.do_change/4
(ecto) lib/ecto/changeset/relation.ex:312: Ecto.Changeset.Relation.map_changes/8
(ecto) lib/ecto/repo/schema.ex:597: anonymous fn/4 in Ecto.Repo.Schema.surface_changes/3
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/repo/schema.ex:584: Ecto.Repo.Schema.surface_changes/3
(ecto) lib/ecto/repo/schema.ex:189: Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/association.ex:556: Ecto.Association.Has.on_repo_change/4
(ecto) lib/ecto/association.ex:338: anonymous fn/7 in Ecto.Association.on_repo_change/6
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/association.ex:335: Ecto.Association.on_repo_change/6
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/association.ex:301: Ecto.Association.on_repo_change/3
(ecto) lib/ecto/repo/schema.ex:708: Ecto.Repo.Schema.process_children/4
(ecto) lib/ecto/repo/schema.ex:774: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
(ecto) lib/ecto/adapters/sql.ex:576: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:1283: DBConnection.transaction_run/4
(db_connection) lib/db_connection.ex:1207: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:798: DBConnection.transaction/3
(ecto) lib/ecto/repo/schema.ex:125: Ecto.Repo.Schema.insert!/4
我通过以下方式运行测试:
iex -S mix test --trace test/services/load_discounts/service_test.exs:42
42是此特定测试开始的行
答案 0 :(得分:0)
我的关联是错误的,我在%Discount中拥有
has_many(:promo_codes, Discount)
我应该有:
has_many(:promo_codes, PromoCode)
令人尴尬!很抱歉!