Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2012 04:38

Revisions

  1. @invalid-email-address Anonymous created this gist Dec 12, 2012.
    37 changes: 37 additions & 0 deletions error.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    DONE Configuring VCR in Rspec for FakeFS
    Loaded fakefs/spec_helpers

    ArgumentError: There is already a cassette with the same name (VCR example group metadata/records an http request). You cannot nest multiple cassettes with the same name.
    /src/cio/vendor/ruby/1.9.1/gems/vcr-2.3.0/lib/vcr.rb:116:in `insert_cassette'
    /src/cio/vendor/ruby/1.9.1/gems/vcr-2.3.0/lib/vcr.rb:166:in `use_cassette'
    /src/cio/vendor/ruby/1.9.1/gems/vcr-2.3.0/lib/vcr/test_frameworks/rspec.rb:83:in `block (2 levels) in configure!'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:93:in `block (2 levels) in run'
    /src/cio/vendor/ruby/1.9.1/gems/vcr-2.3.0/lib/vcr/util/variable_args_block_caller.rb:9:in `call'
    /src/cio/vendor/ruby/1.9.1/gems/vcr-2.3.0/lib/vcr/util/variable_args_block_caller.rb:9:in `call_block'
    /src/cio/vendor/ruby/1.9.1/gems/vcr-2.3.0/lib/vcr.rb:169:in `use_cassette'
    /src/cio/vendor/ruby/1.9.1/gems/vcr-2.3.0/lib/vcr/test_frameworks/rspec.rb:83:in `block (2 levels) in configure!'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:93:in `block (2 levels) in run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:91:in `call'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:91:in `run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:424:in `run_hook'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example_group.rb:327:in `run_around_each_hooks'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example.rb:111:in `run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example_group.rb:388:in `block in run_examples'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example_group.rb:384:in `map'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example_group.rb:384:in `run_examples'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/example_group.rb:369:in `run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:28:in `map'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:28:in `block in run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/reporter.rb:34:in `report'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:25:in `run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/runner.rb:80:in `run'
    /src/cio/vendor/ruby/1.9.1/gems/rspec-core-2.12.1/lib/rspec/core/runner.rb:17:in `block in autorun'

    81 changes: 81 additions & 0 deletions fakefs_persister.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    require 'fileutils'

    module Cio
    class Bdd
    # The only built-in cassette persister. Persists cassettes to the file system.
    class FakeFSFileSystem
    #extend self

    # @private
    attr_reader :storage_location

    def initialize(dir)
    @storage_location=dir
    end

    # @private
    def storage_location=(dir)
    puts 'is this called?'
    ::FakeFS.deactivate! if ::FakeFS.activated?
    ::FileUtils.mkdir_p(dir) if dir
    @storage_location = dir ? absolute_path_for(dir) : nil
    ::FakeFS.activate!
    @storage_location
    end

    # Gets the cassette for the given storage key (file name).
    #
    # @param [String] file_name the file name
    # @return [String] the cassette content
    def [](file_name)
    ::FakeFS.deactivate! if ::FakeFS.activated?
    path = absolute_path_to_file(file_name)
    return nil unless ::File.exist?(path)
    result = ::File.read(path)
    ::FakeFS.activate!
    result
    end

    # Sets the cassette for the given storage key (file name).
    #
    # @param [String] file_name the file name
    # @param [String] content the content to store
    def []=(file_name, content)
    ::FakeFS.deactivate! if ::FakeFS.activated?
    path = absolute_path_to_file(file_name)
    directory = File.dirname(path)
    FileUtils.mkdir_p(directory) unless File.exist?(directory)
    result = File.open(path, 'w') { |f| f.write(content) }
    ::FakeFS.activate!
    result
    end

    # @private
    def absolute_path_to_file(file_name)
    ::FakeFS.deactivate! if ::FakeFS.activated?
    return nil unless storage_location
    result = ::File.join(storage_location, sanitized_file_name_from(file_name))
    ::FakeFS.activate!
    result
    end

    private

    def absolute_path_for(path)
    ::FakeFS.deactivate! if ::FakeFS.activated?
    result = ::Dir.chdir(path) { ::Dir.pwd }
    ::FakeFS.activate!
    result
    end

    def sanitized_file_name_from(file_name)
    ::FakeFS.deactivate! if ::FakeFS.activated?
    parts = file_name.to_s.split('.')
    file_extension = '.' + parts.pop if parts.size > 1
    result = parts.join('.').gsub(/[^\w\-\/]+/, '_') + file_extension.to_s
    ::FakeFS.activate!
    result
    end
    end
    end
    end
    33 changes: 33 additions & 0 deletions fakefs_vcr_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    require 'spec_helper'

    def make_http_request(url)
    ::Typhoeus.get(url).body
    end

    def here(fname)
    RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
    end

    describe "VCR example group metadata",:vcr, :type => :fakefs do
    let(:url){'http://google.com.au/index.html'}
    it 'records an http request' do
    make_http_request(url).should match /The document has moved/
    end

    it 'records another http request' do
    make_http_request(url).should match /The document has moved/
    end

    context 'in a nested example group' do
    it 'records another one' do
    make_http_request(url).should match /The document has moved/
    end
    end
    end

    describe "VCR example metadata" do
    let(:url){'http://google.com/index.html'}
    it 'records an http request', :vcr do
    make_http_request(url).should match /The document has moved/
    end
    end
    54 changes: 54 additions & 0 deletions spec_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    require 'fakeweb'
    require 'typhoeus'
    require 'vcr'

    module ::FakeFSTrigger
    def self.included(base)
    RSpec.configure do |config|
    config.before(:suite) do
    require 'fakefs/spec_helpers'
    config.include ::FakeFS::SpecHelpers
    config.include ::Cio::Helpers::Shared # utility methods unique to BDD, but shared between rspec and cucumber
    config.include ::Cio::Spec::Support::Helpers::Include # utility methods unique to rspec
    config.extend ::Cio::Spec::Support::Helpers::Extend # utility methods unique to rspec
    ::FakeFS.activate!
    puts ' Loaded fakefs/spec_helpers'
    end
    config.after(:suite) do
    ::FakeFS::FileSystem.clear
    ::FakeFS.deactivate!
    end
    end
    VCR.configure do |c|
    begin
    require 'fakefs/spec_helpers'
    puts 'Configuring VCR in Rspec for FakeFS'
    c.allow_http_connections_when_no_cassette = true
    c.cassette_library_dir = 'bdd/cassettes'
    c.cassette_persisters[:file_system] = ::Cio::Bdd::FakeFSFileSystem.new('bdd/cassettes')
    c.default_cassette_options = { :persist_with => :file_system }
    c.hook_into :fakeweb, :typhoeus
    c.configure_rspec_metadata!
    c.ignore_localhost = true
    c.default_cassette_options = { :record => :new_episodes,
    :re_record_interval => 7.days }
    puts 'DONE Configuring VCR in Rspec for FakeFS'
    rescue => e
    puts e.message
    puts e.backtrace.join '\n'

    end
    end
    end
    end

    RSpec.configure do |c|
    # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
    c.mock_with :rr #::RR::Adapters::Rspec
    c.backtrace_clean_patterns.push(::RR::Errors::BACKTRACE_IDENTIFIER)
    c.treat_symbols_as_metadata_keys_with_true_values = true
    c.run_all_when_everything_filtered = true
    c.filter_run :focus
    c.alias_it_should_behave_like_to :it_validates, "it validates"
    c.include ::FakeFSTrigger, :type => :fakefs
    end