-
-
Save mariusbutuc/8a9cf3d940082a25075d to your computer and use it in GitHub Desktop.
MiniTest and VCR: one cassette per test?
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 | |
# cribbed from Rails and modified for VCR | |
# https://github.com/rails/rails/blob/b451de0d6de4df6bc66b274cec73b919f823d5ae/activesupport/lib/active_support/testing/declarative.rb#L9 | |
def test_with_cassette(name, vcr_options = {}, &block) | |
auto_cassette_name = name.gsub(/\s+/, '_') | |
test_name = "test_#{auto_cassette_name}".to_sym | |
if block_given? | |
group = vcr_options.delete(:group) | |
group_prefix = "#{group}/" if group.present? | |
cassette = vcr_options.fetch(:cassette, auto_cassette_name) | |
vcr_options = { tag: group }.merge(vcr_options) if group | |
define_method(test_name) do | |
VCR.use_cassette("#{group_prefix}#{cassette}", vcr_options) do | |
instance_eval &block | |
end | |
end | |
else | |
define_method(test_name) do | |
flunk "No implementation provided for #{name}" | |
end | |
end | |
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
class VeryExampleTest < ActiveSupport::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 'something else', group: :examples do | |
# … | |
end | |
# override tag, override cassette name, supply other VCR options | |
test_with_cassette 'much options', group: :examples, cassette: 'custom_name', tag: :whatever, vcr_options: :supported do | |
# … | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment