我从服务器收到json响应。
{
"@type": "res",
"createdAt": "2014-07-24T15:26:49",
"description": "test",
"disabled": false
}
我如何测试response
isright
或wrong
。以下是我的测试案例。
it "can find an account that this user belongs to" do
Account.find(id: @acc.id, authorization: @token);
expect(response.status).to eq 200
expect(response.body).to eq({
"@type": "res",
"createdAt": "2014-07-24T15:26:49",
"description": "test",
"disabled": false
})
end
当我尝试执行测试时,它引发了很多语法错误。
您应该解析JSON.parse(response.body)
,因为body为字符串:
it "can find an account that this user belongs to" do
Account.find(id: @acc.id, authorization: @token)
valid_hash = {
"@type" => "res",
"createdAt" => "2014-07-24T15:26:49",
"description" => "test",
"disabled" => false
}
expect(response.status).to eq 200
expect(JSON.parse(response.body)).to eq(valid_hash)
end
或不解析:
it "can find an account that this user belongs to" do
Account.find(id: @acc.id, authorization: @token)
valid_hash = {
"@type" => "res",
"createdAt" => "2014-07-24T15:26:49",
"description" => "test",
"disabled" => false
}
expect(response.status).to eq 200
expect(response.body).to eq(valid_hash.to_json)
end
更新,您的hash
语法无效:
valid_hash = {
"@type": "res",
"createdAt": "2014-07-24T15:26:49",
"description": "test",
"disabled": false
}
利用:
valid_hash = {
"@type" => "res",
"createdAt" => "2014-07-24T15:26:49",
"description" => "test",
"disabled" => false
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句