Skip to content

Commit

Permalink
Add VCR
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdabbs committed Nov 22, 2015
1 parent 13d18db commit f2066e9
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -40,3 +40,5 @@ libpeerconnection.log

# Ignore application configuration
/config/application.yml

/fixtures/vcr_cassettes
1 change: 1 addition & 0 deletions .rspec
@@ -1,2 +1,3 @@
--color
--require spec_helper
--order random
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -56,11 +56,12 @@ group :test do
gem 'simplecov'

gem 'rspec-rails'
gem 'rspec-its'
gem 'rspec-given'
gem 'capybara'
gem 'factory_girl_rails'
gem 'database_cleaner'
gem 'vcr'
gem 'webmock'
gem 'zonebie'
gem 'timecop'
end
Expand Down
15 changes: 11 additions & 4 deletions Gemfile.lock
Expand Up @@ -97,6 +97,8 @@ GEM
simplecov (~> 0.10.0)
term-ansicolor (~> 1.3)
thor (~> 0.19.1)
crack (0.4.2)
safe_yaml (~> 1.0.0)
database_cleaner (1.5.1)
debug_inspector (0.0.2)
descendants_tracker (0.0.4)
Expand Down Expand Up @@ -139,6 +141,7 @@ GEM
haml (>= 4.0.6, < 5.0)
html2haml (>= 1.0.1)
railties (>= 4.0.1)
hashdiff (0.2.3)
hitimes (1.2.3)
html2haml (2.0.0)
erubis (~> 2.7.0)
Expand Down Expand Up @@ -252,9 +255,6 @@ GEM
rspec-given (3.7.1)
given_core (= 3.7.1)
rspec (>= 2.14.0)
rspec-its (1.2.0)
rspec-core (>= 3.0.0)
rspec-expectations (>= 3.0.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
Expand All @@ -269,6 +269,7 @@ GEM
rspec-support (3.3.0)
ruby_parser (3.7.1)
sexp_processor (~> 4.1)
safe_yaml (1.0.4)
sass (3.4.19)
sass-rails (5.0.4)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -337,13 +338,18 @@ GEM
unf_ext
unf_ext (0.0.7.1)
uniform_notifier (1.9.0)
vcr (3.0.0)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
warden (1.2.3)
rack (>= 1.0)
webmock (1.22.3)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
xpath (2.0.0)
nokogiri (~> 1.3)
zonebie (0.5.1)
Expand Down Expand Up @@ -381,7 +387,6 @@ DEPENDENCIES
rails_12factor
rollbar
rspec-given
rspec-its
rspec-rails
sass-rails
sidekiq
Expand All @@ -394,7 +399,9 @@ DEPENDENCIES
timecop
twilio-ruby
uglifier
vcr
virtus
webmock
zonebie

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion app/services/sms/receiver.rb
Expand Up @@ -12,7 +12,7 @@ def handle from:, body:
twilio_account: twilio,
user: user,
number: from,
text: body,
text: body.strip,
direction: :incoming

response = begin
Expand Down
4 changes: 2 additions & 2 deletions spec.new/controllers/twilio_controller_spec.rb
Expand Up @@ -4,15 +4,15 @@
Given(:twilio) { TwilioAccount.first }
Given(:phone) { FactoryGirl.create :phone }

context "with a valid sid" do
context "with a valid sid", :vcr do
When(:result) { post :receive, AccountSid: twilio.sid, From: phone.number, To: twilio.number, Body: "help" }

Then { result.status == 200 }
And { SMS.outgoing.count == 1 }
And { SMS.outgoing.last.number == Phone.condense(phone.number) }
end

context "with a new phone number" do
context "with a new phone number", :vcr do
Given(:number) { "+1 (555) 555-0123" }
When(:result) { post :receive, AccountSid: twilio.sid, From: number, To: twilio.number, Body: "help" }

Expand Down
2 changes: 1 addition & 1 deletion spec.new/features/sms_request_spec.rb
@@ -1,7 +1,7 @@
require "rails_helper"

describe "Ordering via sms" do
it "runs end-to-end" do
it "runs end-to-end", :vcr do
phone = FactoryGirl.create :phone
volunteer = phone.user
country = volunteer.country
Expand Down
13 changes: 13 additions & 0 deletions spec.new/rails_helper.rb
Expand Up @@ -27,6 +27,11 @@
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

VCR.configure do |config|
config.cassette_library_dir = "fixtures/vcr_cassettes"
config.hook_into :webmock
end

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
Expand Down Expand Up @@ -59,4 +64,12 @@
# * for some reason, `only` seems to work here, but `except` is being ignored
# DatabaseCleaner.clean_with :truncation, except: %w( countries supplies country_supplies twilio_account )
end

# TODO: many of these tests don't really need to hit Twilio and should probably opt out
config.around :each, :vcr do |x|
name = x.full_description.gsub /\W+/, '-'
VCR.use_cassette name, re_record_interval: 1.week do
x.run
end
end
end
4 changes: 2 additions & 2 deletions spec.new/services/sms_receiver_spec.rb
Expand Up @@ -5,14 +5,14 @@
Given(:phone) { FactoryGirl.create :phone }
Given(:receiver) { SMS::Receiver.new sid: twilio.sid, to: twilio.number }

context "with a message" do
context "with a message", :vcr do
When(:result) { receiver.handle from: phone.number, body: "got it" }

Then { result == SMS.outgoing.last }
And { result.text =~ /can't find any outstanding orders/i }
end

context "with an empty message" do
context "with an empty message", :vcr do
When(:result) { receiver.handle from: phone.number, body: "" }

Then { result == SMS.outgoing.last }
Expand Down

0 comments on commit f2066e9

Please sign in to comment.