A common source of difficulty when using VCR with the default matchers are non-deterministic URIs. If the URI changes on every test run (because it includes a timestamp parameter, or whatever), the default URI matcher will not work well for you.
You can write a custom matcher to match URIs however you want, but for the common need to match on a URI and ignore particular query parameters, VCR provides an easier way:
Match the URI on all but the timestamp query parameter
And a file named "uri_without_param_matcher.rb" with:
include_http_adapter_for("net/http")
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
c.default_cassette_options = {
:match_requests_on => [:method,
VCR.request_matchers.uri_without_param(:timestamp)]
}
end
def search_uri(q)
"http://example.com/search?q=#{q}×tamp=#{Time.now.to_i}"
end
VCR.use_cassette('example') do
puts "Response for bar: " +
response_body_for(:get, search_uri("bar"))
end
VCR.use_cassette('example') do
puts "Response for foo: " +
response_body_for(:get, search_uri("foo"))
end
When I run ruby uri_without_param_matcher.rb
Then it should pass with:
Response for bar: bar response
Response for foo: foo response