这是我的rspec代码: -
it "should match with valid address" do
get :index, Devise.token_authentication_key => @user.authentication_token, business_id: @business2.id
expect(response.status).to eq(200)
expect(response.body).to eq([@location].to_json(LocationFinder::API_PARAMS.merge(:root => false)))
end
预期:
"[
{
\"address\":\"1120 Milky Way\",
\"business_id\":1,
\"city\":\"Cupertino\",
\"latitude\":\"2.4\",
\"longitude\":\"2.9\",
\"name\":\"Joe's Diner\"
}
]"
得到:
"[
{
\"address\":\"1120 Milky Way\",
\"business_id\":1,
\"city\":\"Cupertino\",
\"latitude\":\"2.4\",
\"longitude\":\"2.9\",
\"name\":\"Joe's Diner\",
\"distance\":712.7948793
}
]"
如何将“距离”字段移除到包含哈希值的数组中。所以我的测试会成功。
答案 0 :(得分:3)
您可以使用key
方法从该哈希中删除delete
,例如:
require 'json'
a = "[
{
\"address\":\"1120 Milky Way\",
\"business_id\":1,
\"city\":\"Cupertino\",
\"latitude\":\"2.4\",
\"longitude\":\"2.9\",
\"name\":\"Joe's Diner\",
\"distance\":712.7948793
}
]"
a = JSON.parse(a)
a[0].delete "distance"
p a
#output:
# [{"address"=>"1120 Milky Way", "business_id"=>1, "city"=>"Cupertino", "latitude"=>"2.4", "longitude"=>"2.9", "name"=>"Joe's Diner"}]