我正在使用谷歌地圖寶石。
我試圖模擬/存根 api 請求失敗。
module GoogleMap
class Route
attr_accessor :start_location, :end_location
def initialize(start_location, end_location)
@start_location = start_location
@end_location = end_location
end
def driving_duration_in_seconds
route.duration.value
end
def driving_distance_in_meters
route.distance.value
end
def driving_distance_hash
return unless start_location && end_location
{ distance_in_meters: driving_distance_in_meters, duration_in_seconds: driving_duration_in_seconds }
end
private
def coordinates_as_strings(location)
"#{location.latitude},#{location.longitude}"
end
def route
@route ||= Google::Maps.route(coordinates_as_strings(start_location), coordinates_as_strings(end_location))
end
end
end
我需要存根:
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET https://maps.googleapis.com/maps/api/directions/json?destination=37.19687112,116.43791248&key=<apikey>&language=en&origin=2.15362819,-81.63712649 with headers {'Accept'=>'*/*', 'Date'=>'Sat, 12 Feb 2022 21:35:55 GMT', 'User-Agent'=>'HTTPClient/1.0 (2.8.3, ruby 2.7.2 (2020-10-01))'}
You can stub this request with the following snippet:
stub_request(:get, "https://maps.googleapis.com/maps/api/directions/json?destination=37.19687112,116.43791248&key=<apikey>&language=en&origin=2.15362819,-81.63712649").
with(
headers: {
'Accept'=>'*/*',
'Date'=>'Sat, 12 Feb 2022 21:35:55 GMT',
'User-Agent'=>'HTTPClient/1.0 (2.8.3, ruby 2.7.2 (2020-10-01))'
}).
to_return(status: 200, body: "", headers: {})
如果我嘗試最基本的存根,我會收到一個錯誤:
stub_request(:any, /maps.googleapis.com/).
to_return(status: 200, body: '', headers: {})
Google::Maps::InvalidResponseException:
unknown error: 783: unexpected token at ''
# .../gems/ruby-2.7.2/gems/google-maps-3.0.7/lib/google_maps/api.rb:64:in `rescue in response'
# .../gems/ruby-2.7.2/gems/google-maps-3.0.7/lib/google_maps/api.rb:60:in `response'
# .../.rvm/gems/ruby-2.7.2/gems/google-maps-3.0.7/lib/google_maps/api.rb:27:in `query'
我認為這是錯誤的,因為我沒有傳遞密鑰。但我不明白為什么我必須將有效的 api 密鑰傳遞給 webmock。
我也不會用任何東西定義我的路線。為了測驗route可以回傳route.distance.value等,我需要用一些東西來模擬。
對于其他測驗,我成功地模擬了實體,但是為了測驗這個庫是否真正有效,我覺得模擬實體方法而不是實際呼叫 api 是浪費測驗。也許這只是浪費時間,我應該假設它有效,因為我正在使用寶石。
但我期待這樣的事情:
RSpec.describe GoogleMap do
let(:start_location) { create(:location) }
let(:end_location) { create(:location) }
context 'GoogleMaps::Route.new(start_location, end_location)' do
let(:subject) { GoogleMap::Route.new(start_location, end_location) }
# I have not been successful in stubbing this with the correct regex
# stub_request(:get, "https://maps.googleapis.com/maps/api/directions/json?destination=<lat>,<long>key=<key>&language=en&origin=<lat>,<long>").
# with(
# headers: {
# 'Accept'=>'*/*',
# 'Date'=>'Thu, 10 Feb 2022 21:09:02 GMT',
# 'User-Agent'=>'HTTPClient/1.0 (2.8.3, ruby 2.7.2 (2020-10-01))'
# }).
# to_return(status: 200, body: "", headers: {})
# stub_request(:get, %r{https:\/\/maps\.googleapis\.com\/maps\/api\/directions\/json\?destination=. ,. &key=. &language=en&origin=. ,. }).
# stub_request(:any, /maps.googleapis.com/).
# to_return(status: 200, body: '', headers: {})
xit 'gives driving distance in seconds'
xit 'gives driving duration in meters'
end
end
uj5u.com熱心網友回復:
您的 WebMock 作業正常。Google::Maps::InvalidResponseException在 WebMock 替換網路呼叫后引發。在引發例外時,Google Maps API 客戶端正在嘗試決議網路呼叫回傳的內容,即''.
它期望回傳一些有效的 JSON。如果你有你的模擬回報{}應該超過那條線。不過,稍后它很可能會偶然發現其他一些例外,因為 gem 需要某種模式。
如果你想繼續沿著這條路走下去,你可以挖掘出來并添加一個有效的回應。但是,我建議不要模擬網路請求,因為這是第三方代碼片段的實作細節,可能隨時更改 - 使您的測驗失敗。相反,我會模擬Google::Maps.route回傳你需要的東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/426361.html
