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
@myronmarston
Copy link

Looks great!

One thing I'd point out is that include VCR::CustomPlaceholders isn't doing what you probably think it is. It looks to me like your goal with using that module is to provide helper methods that are available to the hooks but aren't simply defined on the top-level object (as that would make them globally available). However, in the context of the configure block, self is still the top-level main object, so you're still adding those methods to every object.

An alternate approach you might consider is using lambdas for those: then they are just local variables. You could make that work like so:

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|
  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

Note that since these are lambdas, you need to use #call or #[] to invoke them. I used the bracket syntax above since it mirrors the original paren syntax well.

@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