Sha256: bb90f02975c0d1b9241e02bd036f3af2c1224dfe8ca9632a57fa203c63d2e6f3

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require 'vcr'
VCR.configure do |config|
  config.cassette_library_dir = 'spec/data/vcr'
  config.hook_into :webmock
end

# example needs to use real_settings if vcr_record: true is used
def real_settings
  config_path = ENV['TROLLOLO_CONFIG_PATH'] || File.expand_path('~/.trollolorc')
  Settings.new(config_path)
end

def real_settings_needed?(example)
  example.metadata[:vcr_record] && Trello.configuration.developer_public_key == 'mykey'
end

def cassette_path(cassette)
  File.join(VCR.configuration.cassette_library_dir, cassette + '.yml')
end

def vcr_record?(example)
  example.metadata[:vcr_record]
end

def vcr_record_mode(example)
  return :all if vcr_record?(example)
  :none
end

def vcr_replace_tokens(cassette_path)
  settings = real_settings
  text = File.read(cassette_path)
  File.open(cassette_path, 'w') do |f|
    text.gsub!(settings.member_token, 'mytoken')
    text.gsub!(settings.developer_public_key, 'mykey')
    f.print text
  end
end

RSpec.configure do |c|
  c.around do |example|
    if (cassette = example.metadata[:vcr])
      raise 'you need to use real_settings to re-record vcr data' if real_settings_needed?(example)
      VCR.use_cassette(cassette, record: vcr_record_mode(example)) do
        example.run
      end
      vcr_replace_tokens(cassette_path(cassette)) if vcr_record?(example)
    else
      example.run
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trollolo-0.3.1 spec/unit/support/vcr.rb
trollolo-0.3.0 spec/unit/support/vcr.rb
trollolo-0.2.0 spec/unit/support/vcr.rb