だから私は基本的に私のjson結果をテストするためにrspecを書こうとしています:
patient_allergies = patient.patient_allergies
expect(response_body['allergies'].size).to eq(patient_allergies.size)
patient_allergies.each_with_index do |allergy, index|
expect(response_body['allergies'][index]['name']).to eq(allergy.name)
allergy.patient_allergy_reactions.each_with_index do |reaction, index|
expect(response_body['reactions'][index]['name']).to eq(reaction.name)
end
end
上記の表は、患者アレルギーと患者アレルギー反応です。
上記のテストは正常に機能します。しかし問題は、インデックスで比較していることです。
json の順序が変わると、テストは失敗します。このためのテストを作成するより良い方法はありますか? 私のjsonは次のようになります:
"allergies": [
{
"name": "Allergy1",
"reactions": [
]
},
{
"name": "Allergy2",
"reactions": [
{
"name": "Reaction1",
"severity": "Medium"
}
]
}
],