Sha256: 940962f85ef0d341b3b2ad2cabe35f02e2c80773f372a7db58e4db3c3374e05e

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

require "vcr"
require "minispec-metadata"

module MinitestVcr
  module Spec

    def self.configure!
      run_before = lambda do |example|
        if metadata[:vcr]
          options = metadata[:vcr].is_a?(Hash) ? metadata[:vcr] : {}
          VCR.insert_cassette StringHelpers.vcr_path(example), options
        end
      end

      run_after = lambda do |example|
        ::VCR.eject_cassette if metadata[:vcr]
      end

      ::MiniTest::Spec.before :each, &run_before
      ::MiniTest::Spec.after :each, &run_after
    end

  end # Spec

  module StringHelpers

    def self.vcr_path(example)
      description_stack(example).push(extract_example_description(example)).join("/")
    end

    protected

    def self.description_stack(example)
      frame = example.class
      stack = []

      while frame != Minitest::Spec do
        stack.unshift frame.desc.to_s
        frame = frame.superclass
      end

      return stack
    end

    # Minitest::Spec takes the example description and writes
    # a test_NNNN_ in front of it, and doesn't actually keep
    # the original anywhere. Okay, we'll take it out.
    def self.extract_example_description(example)
      if (example.name =~ /\Atest_\d{4}_(.*)\z/)
        return $1
      else
        return example.name
      end
    end

  end
end # MinitestVcr

if defined? ActiveSupport::TestCase
  class ActiveSupport::TestCase
    def before_setup
      super
      if self.class.name.match("::vcr::")
        base_path = self.class.name.split("::")
          .map {|p| p.underscore.gsub(" ", "_") unless p == "vcr" }.join("/")

        file_name = name.gsub(/^test_.\d+_/, " ").strip.gsub(" ", "_")
        VCR.insert_cassette(base_path + "/" + file_name)
      end
    end

    def after_teardown
      if self.class.name.match("::vcr::")
        ::VCR.eject_cassette
      end
      super
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
minitest-vcr-1.4.0 lib/minitest-vcr/spec.rb
minitest-vcr-1.3.0 lib/minitest-vcr/spec.rb