[英]How to override delegated attribute in factory_girl?
我有一個委派給關聯對象(AR / Postgres)的屬性,並且在調用工廠時嘗試覆蓋它。
class Patient
delegate :email, to: :credential
has_one :credential, as: :owner, dependent: :destroy
end
class Credential
belongs_to :owner, polymorphic: true
end
FactoryGirl.define do
factory :patient do
first_name 'Jane'
last_name 'Patient'
dob Date.parse('2005-01-11')
gender 'Female'
zipcode '80202'
after(:build) do |user|
create(:credential, owner: user)
end
end
end
FactoryGirl.define do
factory :credential do
password '!Secret15'
password_confirmation { password }
agree_to_terms true
sequence :email do |n|
"user#{n}@bar.com"
end
end
end
並致電:
create(:patient, email: 'new@email.com')
它根本不起作用,並且屬性已按默認順序設置。
搜尋他們的文檔和Google,但找不到答案。 任何幫助是極大的贊賞。
factory_girl對delegate
不做任何事情。 您需要自己將email
傳遞email
:credential
工廠。 屬性在回調中不可用,因此請從評估器獲取email
,該評估器是回調塊的第二個參數:
FactoryGirl.define do
factory :patient do
# other attributes
after(:build) do |user, evaluator|
create(:credential, owner: user, email: evaluator.email)
end
end
end
在這里記錄: https : //github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#transient-attributes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.