Rails: Using dynamic "interpolations" in VCR
Mittwoch, 22. Februar 2017, 09:03 Uhr | roberto@vasquez-angel.de |When using the vcr gem, you may encounter the problem, that you need to have variables in your requests. For example, your colleagues use different hostnames for local api endpoints. Then you have the problem, that a recorded cassette does not work everywhere.
VCR has a configuration option that is called define_cassette_placeholder. With this option you can define a string and a code block, that will be run replacing this string in your cassette.
Here is a simplified example:
# spec/support/vcr.rb
VCR.configure do |c|
c.define_cassette_placeholder("<API_HOST>") do
hosts = {
'default' => 'my_app.ldev'
'kara.thrace' => 'my_app.localdev',
'bill.adama' => 'my_app.dev'
}
user = ENV.fetch('USER') { default }
hosts[user]
end
end
# spec/fixtures/vcr_cassettes/example.yml
...
http_interactions:
- request:
method: get
uri: http://<API_HOST>/api/v1/cic/dradis_status.json