Skip to content

Instantly share code, notes, and snippets.

@tommeier
Forked from myronmarston/some_spec.rb
Created April 17, 2012 01:45
Show Gist options
  • Save tommeier/2402838 to your computer and use it in GitHub Desktop.
Save tommeier/2402838 to your computer and use it in GitHub Desktop.
VCR with placeholders
use_vcr_cassette 'some/cassette', :tag => :bad_staging_api
set_wildcards_for_response = lambda do |response|
wildcards = {
'<CHECK_IN>' => %w( InDate EffectiveStartDate ),
'<CHECK_OUT>' => %w( OutDate EffectiveEndDate ),
'<TOTAL_RATE>' => %w( TotalRate )
}
wildcards.each do |wildcard, attributes|
attributes.each {|attribute| response.body.gsub!(/#{attribute}\=["']([^"']+)["']/, "#{attribute}=\"#{wildcard}\"") }
end
response
end
replace_wildcards = lambda do |response|
response.body.gsub!('<CHECK_IN>', Time.zone.today.to_s(:db))
response.body.gsub!('<CHECK_OUT>', (Time.zone.today + 4.days).to_s(:db))
response.body.gsub!('<TOTAL_RATE>', "836.00")
response
end
VCR.configure do |c|
include VCR::CustomPlaceholders
c.cassette_library_dir = Rails.root.join('spec/fixtures/vcr_cassettes')
c.hook_into :webmock
c.default_cassette_options = { :record => :new_episodes }
newly_recorded_bad_staging_api_request = lambda do |req|
VCR.current_cassette &&
VCR.current_cassette.tags.include?(:bad_staging_api) &&
req.recordable?
end
#Ensure first test run applies forced test values (irrelevent content such as date)
c.after_http_request(newly_recorded_bad_staging_api_request) do |req, res|
set_wildcards_for_response[res]
replace_wildcards[res]
end
c.before_record(:bad_staging_api) do |interaction, cassette|
set_wildcards_for_response[interaction.response]
end
# Needed for subsequent test runs...
c.before_playback(:bad_staging_api) do |interaction|
replace_wildcards[interaction.response]
end
end
@tommeier
Copy link
Author

Perfect, i started down that path but screwed it up by putting it within the VCR.configure context, and not using the #[] syntax. Much cleaner now, and working perfectly in the app.

@tommeier
Copy link
Author

Example gist edited to reflect updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment