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

时间:2015-04-19 21:32:26

标签: ruby-on-rails integration-testing

此测试一直失败我不知道该怎么做。我猜测修复将是渲染原始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']

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:-1)

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