Last active
August 29, 2015 14:00
VCR cassettes relative to the current describe/context block
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
require 'spec_helper' | |
module Module1 | |
describe Module2::MyClass do | |
let(:my_instance) { Module1::Module2::MyClass.new('abc', 123) } | |
context 'super cool context' do | |
# Use cassette Module1_Module2_MyClass/super_cool_context/neat_method.yml | |
describe 'neat_method', vcr: relative_cassette('neat_method') do | |
subject { my_instance.neat_method(id) } | |
it 'does something' do | |
# ... | |
end | |
it 'does something else' do | |
# ... | |
end | |
end | |
end | |
end | |
end |
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
# spec/support/vcr_cassette_helpers.rb | |
# Returns VCR options specifying the path to the given cassette, found in a | |
# directory for the current context/describe block. | |
def relative_cassette cassette_name | |
context = get_rspec_context(metadata) | |
# 'Module1_Module2_MyClass/super_cool_context' | |
path_to_cassette = context.map {|name| name.gsub(/::/, '_').gsub(/\s/, '_') }. | |
join('/') | |
{cassette_name: path_to_cassette + '/' + cassette_name} | |
end | |
# e.g., ["Module1::Module2::MyClass", "super cool context"] | |
def get_rspec_context metadata, context=[] | |
if metadata.has_key?(:example_group) | |
get_rspec_context metadata[:example_group], context | |
end | |
context << metadata[:description_args] | |
context.reject(&:blank?).map {|args| args.first.to_s } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment