【发布时间】:2013-03-23 18:06:32
【问题描述】:
我有以下工厂用于处理 Patient_allergies
FactoryGirl.define do
factory :patient_allergy do
patient
name 'Peanuts'
end
end
以下工厂用于patient_allergy_reactions
FactoryGirl.define do
factory :patient_allergy_reaction do
patient_allergy
name 'Fever'
severity 'High'
end
end
patient_allergy 的模型如下所示:
class PatientAllergy < ActiveRecord::Base
belongs_to :patient
has_many :patient_allergy_reactions
end
patient_allergy_reaction 的模型如下所示:
class PatientAllergyReaction < ActiveRecord::Base
belongs_to :patient_allergy
end
我的模型测试如下所示:
it 'returns correct allergies with reactions' do
#create an allergy
allergy_name = 'Peanuts'
patient_allergy = create(:patient_allergy, name: allergy_name, patient: patient)
#create a allergy reaction
reaction_name = 'Fever'
reaction_severity = 'Low'
allergy_reaction = create(:patient_allergy_reaction, name: reaction_name, severity: reaction_severity, patient_allergy: patient_allergy)
expect(patient.patient_allergies.size).to eq(1)
expect(patient.patient_allergies[0]).to eq(patient_allergy)
expect(patient.patient_allergies[0].patient_allergy_reactions[0]).to eq(allergy_reaction)
end
以上工作正常,但似乎没有增加太多价值。 我正在尝试找出一种在上述测试中使用构建和特征的方法。 否则,有没有办法使用 expect(patient).to have_many(:patient_allergies) 匹配器之类的。
如果我能理解用工厂女孩测试我的模型,那将非常有帮助。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 rspec factory-bot rspec-rails