From 458f55a73105d8ade30f9874e8ca6b82e86011f1 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Sun, 13 May 2018 11:21:12 +0200 Subject: Define a dummy TomTom api_key in spec --- lib/tom_tom.rb | 28 ++++++++++++++----------- spec/services/route_way_cost_calculator_spec.rb | 4 +++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb index fcebcc7ac..9158b7a28 100644 --- a/lib/tom_tom.rb +++ b/lib/tom_tom.rb @@ -1,26 +1,30 @@ module TomTom BASE_URL = 'https://api.tomtom.com' - @api_key = Rails.application.secrets.tomtom_api_key - @connection = Faraday.new( - url: BASE_URL, - params: { - key: @api_key - } - ) do |faraday| - faraday.use FaradayMiddleware::FollowRedirects, limit: 1 - faraday.adapter Faraday.default_adapter + @@api_key = Rails.application.secrets.tomtom_api_key + cattr_accessor :api_key + + def self.connection + @connection ||= Faraday.new( + url: BASE_URL, + params: { + key: api_key + } + ) do |faraday| + faraday.use FaradayMiddleware::FollowRedirects, limit: 1 + faraday.adapter Faraday.default_adapter + end end def self.enabled? - @api_key.present? + api_key.present? end def self.batch(way_costs) - TomTom::Batch.new(@connection).batch(way_costs) + TomTom::Batch.new(connection).batch(way_costs) end def self.matrix(way_costs) - TomTom::Matrix.new(@connection).matrix(way_costs) + TomTom::Matrix.new(connection).matrix(way_costs) end end diff --git a/spec/services/route_way_cost_calculator_spec.rb b/spec/services/route_way_cost_calculator_spec.rb index d210a6b6e..d11db2950 100644 --- a/spec/services/route_way_cost_calculator_spec.rb +++ b/spec/services/route_way_cost_calculator_spec.rb @@ -3,13 +3,15 @@ RSpec.describe RouteWayCostCalculator do it "calculates and stores WayCosts in the given route's #cost field" do route = create(:route) + allow(TomTom).to receive(:api_key).and_return('dummy') + # Fake the request to the TomTom API, but don't actually send the right # things in the request or response. This is just to fake the request so # we don't actually call their API in tests. The test doesn't test # anything given in the response. stub_request( :post, - "https://api.tomtom.com/routing/1/matrix/json?key&routeType=shortest&traffic=false&travelMode=bus" + "https://api.tomtom.com/routing/1/matrix/json?key=dummy&routeType=shortest&traffic=false&travelMode=bus" ) .with( headers: { -- cgit v1.2.3