Factory Girl:想要防止工厂女孩因某些特征而回调
Factory Girl: want to prevent factory girl callbacks for some traits
我有一个 FactoryGirl
模型 class。在这个模型中,我定义了一些特征。在某些特性中,我不希望 FactoryGirl 回调调用,但我不知道如何。例如这里是我的代码:
FactoryGirl.define do
factory :product do
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
after :create do |product|
FactoryGirl.create_list :product_details, 1, :product => product
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
在此代码中,我不想 :special_product
特征调用 after :create
。我不知道该怎么做。
@Edit: 我想要这样做的原因是因为有时我想从 parent -> children 生成数据。但有时我希望从 children 到 parent 反之亦然。因此,当我从 children -> parent 开始时,parent 处的回调被调用,因此 children 被创建了两次。那不是我想要的。
@Edit 2: 我的问题是防止来自 FactoryGirl 的回调,而不是来自 ActiveRecord 模型的回调。
谢谢
您可以使用 transient attributes 来实现。
喜欢:
factory :product do
transient do
create_products true
end
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
after :create do |product, evaluator|
FactoryGirl.create_list(:product_details, 1, :product => product) if evaluator.create_products
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
但我认为模拟此问题的更好方法是为 "base case" 定义一个 trait
或拥有多个工厂。
您可以使用 Factory Girl 文档中描述的相同方法 has_many
relationship:
factory :product_detail do
product
#... other product_detail attributes
end
factory :product do
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
factory :product_with_details do
transient do
details_count 1 # to match your example.
end
after(:create) do |product, evaluator|
create_list(:product_detail, evaluator.details_count, product: product)
end
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
这允许您为 parent->children:
生成数据
create(:product_with_details) # creates a product with one detail.
create(:product_with_details, details_count: 5) # if you want more than 1 detail.
...对于特殊产品
# does not create any product_details.
create(:product)
create(:product, :special_product)
为children->parent
生成
create(:product_detail)
我有一个 FactoryGirl
模型 class。在这个模型中,我定义了一些特征。在某些特性中,我不希望 FactoryGirl 回调调用,但我不知道如何。例如这里是我的代码:
FactoryGirl.define do
factory :product do
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
after :create do |product|
FactoryGirl.create_list :product_details, 1, :product => product
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
在此代码中,我不想 :special_product
特征调用 after :create
。我不知道该怎么做。
@Edit: 我想要这样做的原因是因为有时我想从 parent -> children 生成数据。但有时我希望从 children 到 parent 反之亦然。因此,当我从 children -> parent 开始时,parent 处的回调被调用,因此 children 被创建了两次。那不是我想要的。
@Edit 2: 我的问题是防止来自 FactoryGirl 的回调,而不是来自 ActiveRecord 模型的回调。
谢谢
您可以使用 transient attributes 来实现。
喜欢:
factory :product do
transient do
create_products true
end
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
after :create do |product, evaluator|
FactoryGirl.create_list(:product_details, 1, :product => product) if evaluator.create_products
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
但我认为模拟此问题的更好方法是为 "base case" 定义一个 trait
或拥有多个工厂。
您可以使用 Factory Girl 文档中描述的相同方法 has_many
relationship:
factory :product_detail do
product
#... other product_detail attributes
end
factory :product do
sequence(:promotion_item_code) { |n| "promotion_item_code#{n}" }
factory :product_with_details do
transient do
details_count 1 # to match your example.
end
after(:create) do |product, evaluator|
create_list(:product_detail, evaluator.details_count, product: product)
end
end
trait :special_product do
# do some thing
# and don't want to run FactoryGirl callback
end
end
这允许您为 parent->children:
生成数据create(:product_with_details) # creates a product with one detail.
create(:product_with_details, details_count: 5) # if you want more than 1 detail.
...对于特殊产品
# does not create any product_details.
create(:product)
create(:product, :special_product)
为children->parent
生成create(:product_detail)