Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Last active January 2, 2016 17:39
Show Gist options
  • Save mfpiccolo/8338565 to your computer and use it in GitHub Desktop.
Save mfpiccolo/8338565 to your computer and use it in GitHub Desktop.
Dynamically creates the file structure and file naming for VCR cassettes with MiniTest::Spec
# Put this in your test helper
class MiniTest::Spec
before :each do |example|
if metadata[:vcr]
test_info = example.class.name.split("::").map {|e| e.underscore}.reject(&:empty?)
name = spec_name.underscore.gsub(/[^\w\/]+/, "_")
path = "test/cassettes/" + [(test_info[0] + "_test"), test_info[1]].join("/")
FileUtils.mkdir_p(path) unless File.exists?(path)
VCR.configure do |c|
c.cassette_library_dir = path
end
VCR.insert_cassette name
end
end
after :each do
if metadata[:vcr]
VCR.eject_cassette
VCR.configure do |c|
c.cassette_library_dir = 'test/cassettes'
end
end
end
# And in you gemfile
# gem "minispec-metadata"
# Example:
describe Foo, vcr: true do
describe "#random_method" do
describe "with some other info" do
before do
# Do some setup
end
it "should be all awesome" do
# Make assertations
end
end
end
# This will create
# test -
# cassettes -
# foo_test -
# #random_method -
# should_be_all_awesome.yml
@smoil
Copy link

smoil commented Jan 9, 2014

Very nice.

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