-
-
Save 3den/1557591 to your computer and use it in GitHub Desktop.
RSpec.configure do |config| | |
# Add VCR to all tests | |
config.around(:each) do |example| | |
name = example.metadata[:full_description].split(/\s+/, 2).join("/").underscore.gsub(/[^\w\/]+/, "_") | |
options = example.metadata[:vcr] || {} | |
VCR.use_cassette(name, options, &example) | |
end | |
end |
I agree with your caveats but this approach is useful for the project I'm in coz there are many developers who would forget to add :vcr to the specs
How do you have VCR configured? VCR, under it's normal configuration, is designed to not let you forget. When an HTTP request is made and there is no current cassette it raises an error to force you to do something about it.
BTW, you can simplify VCR.use_cassette(name, options) {example.call}
to VCR.use_cassette(name, options, &example)
.
my config is:
VCR.configure do |c|
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :webmock
c.default_cassette_options = { :record => :new_episodes, :re_record_interval => 7.days }
c.allow_http_connections_when_no_cassette = false
end
Hmm...allow_http_connections_when_no_cassette = false
(which is the default, BTW) should make it so that you get an exception anytime an HTTP request is made when there is no cassette. Do you get the error?
The problem is that the error message is:
TypeError:
can't convert String into Integer
This is hard to debug and I had to puts
the remote call to see the real error
That's a VCR bug we should fix then. Can you file an issue on the VCR issue tracker? It would be really helpful to have the backtrace and a small reproducible example (or access to a project that triggers this error). I've not gotten that error before so your code must be exercising some edge case that mine is not.
I agree with your caveats but this approach is useful for the project I'm in coz there are many developers who would forget to add :vcr to the specs, the implicit approach is more "magical" and a great thing about VCR is that the cassettes are only created when needed. But I will try the metadata to see what is the diference in performance.
Thanks for your feedback!