Last active
January 2, 2016 17:39
-
-
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
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice.