问题
In a Rails app, I'm using FactoryGirl to define a general factory plus several more specific traits. The general case and all but one of the traits have a particular association, but I'd like to define a trait where that association is not created/built. I can use an after
callback to set the association's id
to nil
, but this doesn't stop the association record from being created in the first place.
Is there a way in a trait definition to completely disable the creation/building of an association that has been defined for the factory the trait belongs to?
For example:
FactoryGirl.define do
factory :foo do
attribute "value"
association :bar
trait :one do
# This has the bar association
end
trait :two do
association :bar, turn_off_somehow: true
# foos created with trait :two will have bar_id = nil
# and an associated bar will never be created
end
end
end
回答1:
An association in factory_girl is just an attribute like any other. Using association :bar
sets the bar
attribute, so you can disable it by overriding it with nil
:
FactoryGirl.define do
factory :foo do
attribute "value"
association :bar
trait :one do
# This has the bar association
end
trait :two do
bar nil
end
end
end
回答2:
I tried @Joe Ferris' answer but appears it does not work anymore in factory_bot 5.0.0. I found this related question that referred to the strategy: :null
flag that can be passed to the association like:
FactoryGirl.define do
factory :foo do
attribute "value"
association :bar
trait :one do
# This has the bar association
end
trait :two do
association :bar, strategy: :null
end
end
end
It seems to do the trick now.
Source code looks like it just stops any callbacks like create or build so renders the association to nothing.
module FactoryBot
module Strategy
class Null
def association(runner); end
def result(evaluation); end
end
end
end
来源:https://stackoverflow.com/questions/19865616/disabling-a-factorygirl-association-within-a-trait