2013-04-01 70 views
1

這是我的RSpec代碼: -如何使用來檢查JSON /測試特定領域rspec的

it "which has max value" do 
    get :index, Devise.token_authentication_key => @user.authentication_token, business_id: @business.id, max: '1' 
    expect(request.flash[:alert]).to eq(nil) 
    expect(response.body).to eq([@location].to_json(LocationFinder::API_PARAMS.merge(:root => false))) 
    end 

和測試結果是 -

預計: 「[{\」 地址\ 「:\」 1120銀河系\」,\ 「business_id \」:1,\ 「城\」:\ 「庫比提諾]」

got: "[{\"address\":\"1120 Milky Way\",\"business_id\":1,\"city\":\"Cupertino,\"distance\":260.33452958767384,]" 

這裏距離是一個額外的領域,我怎麼能檢查特定領域或者如果它是不可能,如何消除不被rspec檢查的「距離」字段。

回答

3

你可以使用類似的檢查各個領域:

# get the first entry in the JSON array 
json_response = JSON.parse(response.body).first 

# compare each field 
expect(json_response['address']).to eq(@location.address) 
expect(json_response['business_id']).to eq(@location.business_id) 
expect(json_response['city']).to eq(@location.city) 

當然,你可能需要調整你打電話@location取決於您的實現精確的方法,但是這是它的要點。