我有以下用于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) 匹配器或其他东西。
如果我能理解用工厂女孩测试我的模型,那将非常有帮助。