VCR debugging
VCR can be a great way to record HTTP interactions in a controlled environment and replay those interactions during tests. We don’t want to make actual HTTP requests during a test scenario, so VCR helps us get around that. Sometimes VCR seems to work out of the box, but sometimes it takes trial and error to get a test to recognize cassettes. Below are some errors and possible resolutions.
Common VCR errors and resolutions
❓ Problem: How to record a cassette inside the VA network? The URL is only accessible from inside the VA network.
✅ Resolution: Try using these instructions (private repository) and adjust the script as necessary. Note: you’ll need AWS access to open a tunnel to the fwdproxy.
❓ Problem: A cassette is being used in a test, but the test doesn’t seem to recognize it. It’s failing as if the cassette is not there.
✅ Possible resolution: Ensure the VCR cassette is spelled correctly and with the correct path. Double check the VCR cassette name. Make sure there is no .yml
at the end of a cassette name in the test.
✅ Possible resolution: It could be that there are other VCR cassettes interfering with the one you want to use and can’t find yours. Try using match_requests_on
and specifying a unique attribute in your cassette. The :match_requests_on
option defaults to [:method, :uri, :body]
(those options are set in spec/rails_helper.rb) unless it’s set otherwise in the test. Here’s the request_matching VCR documentation.
This is where the defaults are set in rails_helper.rb
in vets-api:
module VCR
def self.all_matches
%i[method uri body]
end
end
Example of match_requests_on
being used:
it '204s when given a null category' do
VCR.use_cassette('okta/verification-scopes', match_requests_on: %i[method path]) do
get '/services/apps/v0/directory/scopes'
expect(response).to have_http_status(:no_content)
end
end
✅ Possible resolution: Most of the time, many tests can use the same cassette with no problem. But if you are using the same cassette more than once and have an error, try using allow_playback_repeats: true
. This might come into play if the test makes multiple API calls to the same endpoint during the course of a single test. Here’s the playback_repeats VCR documentation.
Example:
it 'prefills 1990' do
VCR.use_cassette('va_profile/military_personnel/service_history_200_many_episodes',
allow_playback_repeats: true, match_requests_on: %i[uri method body]) do
expect_prefilled('22-1990')
end
end
❓ Problem: Generic VCR errors that don’t show where the actual problem is.
✅ Possible resolution: binding.prys for the win! Place binding.pry
statements within any methods or controllers that might reference the endpoint. Sometimes a bug in the code can result in generic VCR errors that make it look like it’s a VCR problem, when it’s not.
General warnings
⚠️ Be sure the cassettes that will be pushed to vets-api don’t have any sensitive information. Link to filter_sensitive_data
VCR documentation. This option is being used in vets-api in config/initializers/integration_recorder.rb. Ask yourself if any more sensitive data needs to be added to this list? If in question, reach out to #vfs-platform-support.
Have any more VCR gotchas? Send them to the Platform using the “Suggest content changes to this page” link below.
Help and feedback
Get help from the Platform Support Team in Slack.
Submit a feature idea to the Platform.