在我的测试中删除对 google 地图 api 的请求
我尝试使用 FakeWeb(在 cucumber/rails 中)模拟地理编码请求。 当我阻止所有 http 请求时,我收到消息:
Real HTTP connections are disabled. Unregistered request: GET
http://maps.google.com/maps/api/geocode/json?..... (FakeWeb::NetConnectNotAllowedError)
因此,我通过尝试注册了 url:
FakeWeb.register_uri(:any, %r|http://maps\.google\.com/maps/|, :json
=> {
"status": "OK",
....}
我收到错误“JSON 文本必须至少包含两个八位字节!” (MultiJson::解码错误) 我不确定要返回什么信息。 FakeWeb 如何返回 json 数据.. 有人有解决方案来消除对谷歌地图 API 的服务器请求吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

“A JSON 文本必须至少包含两个八位字节!”
错误表示您用于存根响应的 JSON 字符串无效。在使用 json 字符串进行存根之前,尝试运行 MultiJson.decode(json_string) ;这应该可以帮助您查明错误。如果您不想处理存根 google 地图 API 的详细信息,您可能需要考虑使用 VCR< /a>;它会为您记录一个真实的响应,并使用它来存根后续测试运行中的请求。这是在测试中获得真实的存根响应的更简单的方法。
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 runningMultiJson.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.