我刚开始使用FactoryGirl进行测试,而且我遇到了一些问题。
我有user
,qualification
和user_qualification
型号。对于qualifications
,我有两个继承的工厂,即qualification_ba
和qualification_phd
。现在,我试图通过运行来生成包含qualification_ba
或qualification_phd
工厂的资格认证:
create(:user_qualification_ba_bad_marks)
。
返回:
NoMethodError: undefined method 'qualification_ba='
。
然而,在跑步时:
create(:qualification_ba)
它按预期成功创建了对象。您可以在多大程度上嵌套工厂?
factory :user do
to_create {|instance| instance.save(validate: false) }
full_name Faker::Name.name
email Faker::Internet.email
password Faker::Internet.password(8)
factory :user_ba_candidate do
before(:create) do |user|
create_list(:user_qualification_ba_bad_marks, 1, user: user)
end
end
factory :user_phd_candidate do
before(:create) do |user|
create_list(:user_qualification_phd_good_marks, 1, user: user )
end
end
end
factory :qualification do
user_selectable true
factory :qualification_ba do
name "Bachelors"
qualification_level 2
end
factory :qualification_phd do
name "Masters"
qualification_level 5
end
end
factory :user_qualification do
to_create {|instance| instance.save(validate: false) }
factory :user_qualification_ba_bad_marks do
qualification_ba
aggregate (30..50).to_a.sample
end
factory :user_qualification_phd_good_marks do
qualification_phd
aggregate (70..90).to_a.sample
end
user
end