Skip to content

Commit

Permalink
Implement VCR request mocking in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bartell committed Jan 18, 2021
1 parent 320902d commit c291716
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 41 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ group :test do
gem 'shoulda-matchers'
gem 'factory_bot_rails'
gem 'faker'
gem 'webmock'
gem 'vcr'
end


Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
arel (9.0.0)
ast (2.4.1)
bootsnap (1.5.1)
msgpack (~> 1.0)
builder (3.2.4)
coderay (1.1.3)
concurrent-ruby (1.1.7)
crack (0.4.5)
rexml
crass (1.0.6)
diff-lcs (1.4.4)
docile (1.3.4)
Expand All @@ -70,6 +74,7 @@ GEM
thor (>= 0.14.0, < 2)
globalid (0.4.2)
activesupport (>= 4.2.0)
hashdiff (1.0.1)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
listen (3.1.5)
Expand Down Expand Up @@ -101,6 +106,7 @@ GEM
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
puma (3.12.6)
racc (1.5.2)
rack (2.2.3)
Expand Down Expand Up @@ -194,6 +200,11 @@ GEM
tzinfo (1.2.9)
thread_safe (~> 0.1)
unicode-display_width (2.0.0)
vcr (6.0.0)
webmock (3.11.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -218,6 +229,8 @@ DEPENDENCIES
shoulda-matchers
simplecov
tzinfo-data
vcr
webmock

RUBY VERSION
ruby 2.5.3p105
Expand Down
62 changes: 62 additions & 0 deletions spec/fixtures/vcr_cassettes/denverco_coord_request.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@
with.library :rails
end
end

VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
c.filter_sensitive_data('<GEOCODE_API_KEY>') { ENV['GEOCODE_API_KEY'] }
c.default_cassette_options = { record: :new_episodes }
end
84 changes: 43 additions & 41 deletions spec/services/geocode_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,63 @@

describe 'GeocodeService' do
it 'can get coordinates from a location string' do
location = 'denver,co'
response = GeocodeService.location_to_coords(location)
VCR.use_cassette('denverco_coord_request') do
location = 'denver,co'
response = GeocodeService.location_to_coords(location)

expect(response).to be_a Hash
expect(response).to have_key :info
expect(response).to have_key :options
expect(response).to have_key :results
expect(response).to be_a Hash
expect(response).to have_key :info
expect(response).to have_key :options
expect(response).to have_key :results

info = response[:info]
info = response[:info]

expect(info).to be_a Hash
expect(info).to have_key :statuscode
expect(info[:statuscode]).to be_an Integer
expect(info[:statuscode]).to eq(0)
expect(info).to have_key :messages
expect(info[:messages]).to be_an Array
expect(info[:messages]).to be_empty
expect(info).to be_a Hash
expect(info).to have_key :statuscode
expect(info[:statuscode]).to be_an Integer
expect(info[:statuscode]).to eq(0)
expect(info).to have_key :messages
expect(info[:messages]).to be_an Array
expect(info[:messages]).to be_empty

options = response[:options]
options = response[:options]

expect(options).to be_a Hash
expect(options).to_not be_empty
expect(options).to be_a Hash
expect(options).to_not be_empty

results = response[:results]
results = response[:results]

expect(results).to be_an Array
expect(results.length).to eq(1)
expect(results).to be_an Array
expect(results.length).to eq(1)

location_results = results.first
location_results = results.first

expect(location_results).to be_a Hash
expect(location_results).to have_key :providedLocation
expect(location_results[:providedLocation]).to be_a Hash
expect(location_results).to be_a Hash
expect(location_results).to have_key :providedLocation
expect(location_results[:providedLocation]).to be_a Hash

expect(location_results[:providedLocation]).to have_key :street
expect(location_results[:providedLocation][:street]).to be_a String
expect(location_results[:providedLocation][:street]).to eq(location)
expect(location_results[:providedLocation]).to have_key :street
expect(location_results[:providedLocation][:street]).to be_a String
expect(location_results[:providedLocation][:street]).to eq(location)

expect(location_results).to have_key :locations
expect(location_results[:locations]).to be_an Array
expect(location_results[:locations]).to_not be_empty
expect(location_results).to have_key :locations
expect(location_results[:locations]).to be_an Array
expect(location_results[:locations]).to_not be_empty

location_data = location_results[:locations].first
location_data = location_results[:locations].first

expect(location_data).to be_a Hash
expect(location_data).to have_key :latLng
expect(location_data).to be_a Hash
expect(location_data).to have_key :latLng

geocoords = location_data[:latLng]
geocoords = location_data[:latLng]

expect(geocoords).to be_a Hash
expect(geocoords).to have_key :lat
expect(geocoords[:lat]).to be_a Float
expect(geocoords[:lat]).to eq(39.738453)
expect(geocoords).to have_key :lng
expect(geocoords[:lng]).to be_a Float
expect(geocoords[:lng]).to eq(-104.984853)
expect(geocoords).to be_a Hash
expect(geocoords).to have_key :lat
expect(geocoords[:lat]).to be_a Float
expect(geocoords[:lat]).to eq(39.738453)
expect(geocoords).to have_key :lng
expect(geocoords[:lng]).to be_a Float
expect(geocoords[:lng]).to eq(-104.984853)
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
SimpleCov.add_filter ['spec']

require 'factory_bot_rails'
require 'webmock/rspec'
require 'vcr'

RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
Expand Down

0 comments on commit c291716

Please sign in to comment.