0

这是我的 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
   }
  ]"

如何将“距离”字段删除到包含哈希值的数组中。所以我的测试会成功。

4

1 回答 1

3

key您可以使用该方法从该哈希中删除 a 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"}]
于 2013-04-01T14:00:40.873 回答