Skip to content

Instantly share code, notes, and snippets.

@3den
Created January 3, 2012 23:38
Show Gist options
  • Save 3den/1557591 to your computer and use it in GitHub Desktop.
Save 3den/1557591 to your computer and use it in GitHub Desktop.
add VRC to all remote calls automatically
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
@myronmarston
Copy link

It's great that it's so easy to integrate VCR and RSpec like this. That said, I wouldn't normally recommend people do it this way, for a few reasons (but if it works great for you--don't let my caveats below discourage you):

  • This adds some extra processing (i.e. inserting the cassette before, and ejecting the cassette after) to each and every example. I don't think it's super slow, but it does add up. I think it's better to just use VCR for the examples that need it.
  • There's no simple way to prevent VCR from being used for a single example here.
  • This is very implicit; I prefer being explicit and tagging the examples that need VCR with metadata.
  • This uses a different cassette for each and every example (since it's based on the example description). Many (most?) times you can use one cassette for multiple examples, which saves hard drive space and keeps your git repository from growing unnecessarily large (which affects, among other things, the time it takes to clone it, and thus may affect your deploy times).

Still, it's a nifty technique--feel free to add to the wiki! I just wouldn't want to make VCR do this since I don't normally recommend this approach. The metadata approach in VCR 2 is very close to this but without these caveats.

@3den
Copy link
Author

3den commented Jan 4, 2012

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!

@myronmarston
Copy link

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).

@3den
Copy link
Author

3den commented Jan 4, 2012

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

@myronmarston
Copy link

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?

@3den
Copy link
Author

3den commented Jan 4, 2012

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

@myronmarston
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment