首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >地理编码的Webmock JSON响应

地理编码的Webmock JSON响应
EN

Stack Overflow用户
提问于 2014-02-17 17:49:13
回答 1查看 1.8K关注 0票数 2

我尝试用webmock来存根一个地理编码的HTTP请求。

但我总是得到这样的错误:

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

代码语言:javascript
复制
  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_map_data.json

代码语言:javascript
复制
{"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"}

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

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2014-02-17 23:41:20

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

代码语言:javascript
复制
{"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"
            }
        ]
    }
}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21825632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档