Skip to content

Instantly share code, notes, and snippets.

@terrcin
Created July 4, 2012 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrcin/3044331 to your computer and use it in GitHub Desktop.
Save terrcin/3044331 to your computer and use it in GitHub Desktop.
Make including VCR in tests real easy
# using the VCR gem:
# https://github.com/myronmarston/vcr/
#
# this wraps each test in a unique VCR cassette and separately the setup method.
#
# require 'vcr_tests' in test_helper.rb
# then in a test file include the VcrTests module after the setup method
module VcrTests
def self.included(base)
base.extend(ClassMethods)
base.class_eval do
class << self
alias_method_chain :test, :vcr
end
end
base.alias_method_chain :setup, :vcr
end
module ClassMethods
# force VCR to be used on all tests, call test_without_vcr to not use it
def test_with_vcr(name, &block)
test_without_vcr(name) do
VCR.use_cassette(self.class.name + '/' + name.gsub(' ', '_')) do
self.instance_eval(&block)
end
end
end
end
def setup_with_vcr
VCR.use_cassette(self.class.name + '/_setup') do
setup_without_vcr
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment