Created
May 17, 2012 22:11
-
-
Save jrochkind/2721883 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class SomethingTest < Test:Unit:TestCase | |
extend TestWithCassette | |
# automatic cassette 'do_something', in | |
# test method test_do_something. | |
test_with_cassette("do something") do | |
# | |
end | |
# optional group name will add a :tag with that group name to use_cassette, | |
# as well as put the cassette in a subdir named after group | |
test_with_cassette("else", :group_name) do | |
# | |
end | |
# override tag, override cassette name, supply other VCR options | |
test_with_cassette("other", :group, :cassette => "something_else", :tag => :whatever, :vcr_options => :supported) do | |
# | |
end | |
end |
This file contains hidden or 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
module TestWithCassette | |
def test_with_cassette(name, group = nil, vcr_options ={}, &block) | |
# cribbed from Rails and modified for VCR | |
# https://github.com/rails/rails/blob/b451de0d6de4df6bc66b274cec73b919f823d5ae/activesupport/lib/active_support/testing/declarative.rb#L9 | |
test_name_safe = name.gsub(/\s+/,'_') | |
test_name = "test_#{test_name_safe}".to_sym | |
defined = instance_method(test_name) rescue false | |
raise "#{test_name} is already defined in #{self}" if defined | |
if block_given? | |
# cassettes in group subdir, if given | |
group_prefix = group ? "#{group}/" : "" | |
# cassette name from test name, can be over-ridden. | |
tape_name = vcr_options.delete(:cassette) || test_name_safe | |
# default tag with groupname, can be over-ridden. | |
vcr_options = {:tag => group}.merge(vcr_options) if group | |
define_method(test_name) do | |
VCR.use_cassette("#{group_prefix}#{tape_name}" , vcr_options) do | |
instance_eval &block | |
end | |
end | |
else | |
define_method(test_name) do | |
flunk "No implementation provided for #{name}" | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment