2014-02-17 3 views
2

Я пытаюсь заглушить запрос геокодирования HTTP с помощью webmock.Webmock Ответ JSON для геокодирования

Но я всегда получаю эту ошибку:

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 FILE /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"} 

Что такое правильный способ ее реализации успешно?

Спасибо!

ответ

0

Я нашел ошибку. Мой файл 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" 
      } 
     ] 
    } 
}} 
Смежные вопросы