【问题标题】:Webmock JSON response for geocoding用于地理编码的 Webmock JSON 响应
【发布时间】:2014-03-16 13:05:31
【问题描述】:

我尝试使用 webmock 存根地理编码 HTTP 请求。

但我总是得到这个错误:

NoMethodError: undefined method `[]' for nil:NilClass

  context "geocoding" do
    before :each do
      @user = Fabricate :user
      stub_request(:get, /.*yboss.yahooapis.com*/).to_return(:body => File.read(File.join("spec", "fixtures", "geocoder", "yahoo_maps_data.json")))

    end
    it 'should geocode if coordinates missing' do

      without_coordinates = Location.new(name: "Test",
                                   street: "Revierstrasse 1",
                                   zipcode: "5020",
                                   country: "AT",
                                   user: @user, affiliate: @user)

      result = without_coordinates.geocode
      without_coordinates.coordinates.should == [47.8029, 13.0472]
    end
end

JSON 文件 /spec/fixtures/geocoder/yahoo_maps_data.json

{"quality": "59",
    "latitude": "47.8029",
    "longitude": "13.0472",
    "offsetlat": "47.802898",
    "offsetlon": "13.04185",
    "radius": "8600",
    "boundingbox": {
        "north": "47.854401",
        "south": "47.7514",
        "east": "13.1075",
        "west": "12.9869"
    }, "name": "",
    "line1": "",
    "line2": "5020 Salzburg",
    "line3": "",
    "line4": "Austria",
    "cross": "",
    "house": "", "street": "",
    "xstreet": "",
    "unittype": "",
    "unit": "",
    "postal": "5020",
    "neighborhood": "",
    "city": "Salzburg",
    "county": "Salzburg",
    "state": "Salzburg",
    "country": "Austria",
    "countrycode": "AT",
    "statecode": "5",
    "countycode": "5",
    "timezone": "Europe/Vienna",
    "uzip": "5020",
    "hash": "",
    "woeid": "12816173",
    "woetype": "11"}

成功实施的正确方法是什么?

谢谢!

【问题讨论】:

    标签: ruby-on-rails json rspec rails-geocoder webmock


    【解决方案1】:

    我发现了错误。我的 JSON 文件不完整:

    {"bossresponse": {
        "responsecode": "200",
        "placefinder": {
            "start": "0",
            "count": "1",
            "request": "flags=JXTSR&gflags=AC&locale=en_US&location=Salzburg",
            "results": [
                {
                    "quality": "40",
                    "latitude": "47.80067",
                    "longitude": "13.04338",
                    "offsetlat": "47.80067",
                    "offsetlon": "13.04338",
                    "radius": "8600",
                    "boundingbox": {
                        "north": "47.85383",
                        "south": "47.75096",
                        "east": "13.12707",
                        "west": "12.98608"
                    },
                    "name": "",
                    "line1": "",
                    "line2": "Salzburg",
                    "line3": "",
                    "line4": "Austria",
                    "cross": "",
                    "house": "",
                    "street": "",
                    "xstreet": "",
                    "unittype": "",
                    "unit": "",
                    "postal": "",
                    "neighborhood": "",
                    "city": "Salzburg",
                    "county": "Salzburg",
                    "state": "Salzburg",
                    "country": "Austria",
                    "countrycode": "AT",
                    "statecode": "5",
                    "countycode": "5",
                    "timezone": "Europe\/Vienna",
                    "uzip": "5020",
                    "hash": "",
                    "woeid": "547826",
                    "woetype": "7"
                }
            ]
        }
    }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多