Created
July 4, 2012 00:21
-
-
Save terrcin/3044331 to your computer and use it in GitHub Desktop.
Make including VCR in tests real easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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