Skip to content

Instantly share code, notes, and snippets.

@Ch4s3
Last active September 11, 2015 16:01
Show Gist options
  • Save Ch4s3/16a6e21bccf16edbd879 to your computer and use it in GitHub Desktop.
Save Ch4s3/16a6e21bccf16edbd879 to your computer and use it in GitHub Desktop.
Rspec, VCR custom request matchers to ignore nondeterministic params
#In your rails_helper.rb
some_api_matcher = VCR.request_matchers.uri_without_param(:token)
VCR.configure do |config|
# use the following option to debug VCR
# config.debug_logger = STDOUT
config.cassette_library_dir = "spec/cassettes"
config.hook_into :webmock, :typhoeus, :faraday
config.default_cassette_options = { record: :new_episodes }
config.allow_http_connections_when_no_cassette = false
config.register_request_matcher :no_random_token, &some_api_matcher
end
#In your test
require "rails_helper"
feature "Filling in a form that calls an api with nondeterministic params", :feature do
describe "The form" do
it "should autopopulate the fields from the api" do
VCR.use_cassette "features/api_form",
match_requests_on:
[:method, :no_random_token] do
visit forms_path
fill_in "person_form_id_field", with: "123456"
click_on "Call API to Get Form"
api_form_value = find_field("some_value[value_name]").value
expect(api_form_value).to eq("This is data from an api with a crazy random token")
#this calls another api but VCR can handle that in 1 cassette
click_on "Submit form"
visit forms_path
click_on "PDF output of the form"
#now we know the processing of second apis response worked
expect(page.body[1..3]).to eq("PDF") #crude, but its a pdf
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment