Stub out a request to google maps api in my tests

1.3k Views Asked by At

I try to mock out geocoding request by using FakeWeb (in cucumber/rails). When I block all http requests I get the message:

Real HTTP connections are disabled. Unregistered request: GET 
http://maps.google.com/maps/api/geocode/json?..... (FakeWeb::NetConnectNotAllowedError)

So I registered the url by trying:

FakeWeb.register_uri(:any, %r|http://maps\.google\.com/maps/|, :json 
=> { 
    "status": "OK", 
....} 

I get the error "A JSON text must at least contain two octets!" (MultiJson::DecodeError) I'm not sure what information to return. And how FakeWeb can return json data.. Does someone have a solution for stubbing out server requests to the google maps api?

1

There are 1 best solutions below

0
Myron Marston On

The "A JSON text must at least contain two octets!" error indicates that the JSON string your are using to stub the response is not valid. Try running MultiJson.decode(json_string) with your json string before stubbing with it; that should help you pinpoint the error.

If you don't want to deal with the details of stubbing the google maps API, you may want to look into using VCR; it will record a real response for you and use that to stub the request on subsequent test runs. It's a much easier way to get realistic stubbed responses in your tests.