-
-
Save tommeier/2402838 to your computer and use it in GitHub Desktop.
VCR with placeholders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use_vcr_cassette 'some/cassette', :tag => :bad_staging_api |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Example gist edited to reflect updates.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.