在 response.body 中渲染完整的 Json 数组

Render the full Json array in response.body

这个测试总是失败我不知道该怎么办。我猜一个修复方法是渲染原始 Json 数组,但我完全迷路了。

错误

NoMethodError:'

中的未定义方法 []' for nil:NilClass /Users/newuser/railschallenge-city-watch/test/api/emergencies_create_test.rb:10:in

紧急情况控制器

def create
    @emergency = Emergency.new(emergency_params)
    if @emergency.save
        render  json: @emergency.to_json , :status => 201
    end 

end


test 'POST /emergencies/ simple creation' do
post '/emergencies/', emergency: { code: 'E-99999999', fire_severity: 1,          police_severity: 2, medical_severity: 3 }
json_response = JSON.parse(response.body)

assert_equal 201, response.status
assert_equal nil, body['message']
assert_equal 'E-99999999', json_response['emergency']['code']
assert_equal 1, json_response['emergency']['fire_severity']
assert_equal 2, json_response['emergency']['police_severity']
assert_equal 3, json_response['emergency']['medical_severity']

结束

试试这个:

render json: @emergency.to_json(:root => "emergency"), :status => 201
render  json: {"emergency" => @emergency}.to_json , :status => 201