所以我的代码是这样返回 json 的:
"offenses": [
{
"name": "Speeding",
"penalties": [
{
"name": "Ticket",
"severity": "Medium"
}
}
]
我有以下胖女孩:
FactoryGirl.define do
factory :person_offense_penalty do
person_offense
name 'Ticket'
severity 'Medium'
end
end
FactoryGirl.define do
factory :person_offense do
name 'Speeding'
person
end
end
这是我对响应的测试,它工作正常
person_offenses = person.person_offenses
expect(response_body['offenses'].size).to eq(person_offenses.size)
person_offenses.each_with_index do |offense, offense_index|
expect(response_body['offenses'][offense_index]['name']).to eq(offense.name)
offense.person_offense_penaltys.each_with_index do |penalty, penalty_index|
expect(response_body['offenses'][offense_index]['penaltys'][penalty_index]['name']).to eq(penalty.name)
expect(response_body['offenses'][offense_index]['penaltys'][penalty_index]['severity']).to eq(penalty.severity)
end
end
我需要编写另一个测试来确保攻击名称按升序排序。有人可以帮我吗