Permalink
Browse files

Improved RSpec support.

Added #use_vcr_cassette macro method that sets up a cassette for an example group.
  • Loading branch information...
1 parent 89748b9 commit f4cce4cdec934a48706830b7ab41d360c953fa46 @myronmarston committed Sep 20, 2010
Showing with 137 additions and 0 deletions.
  1. +2 −0 CHANGELOG.md
  2. +50 −0 features/rspec.feature
  3. +3 −0 features/step_definitions/vcr_steps.rb
  4. +35 −0 lib/vcr/rspec.rb
  5. +47 −0 spec/rspec_spec.rb
View
@@ -6,6 +6,8 @@
new recorded interactions. Previously, all of the previously recorded interactions were deleted.
* Added `:re_record_interval` cassette option. This option causes a cassette to be re-recorded when the
existing file is older than the specified interval.
+* Improved RSpec support. Added #use_vcr_cassette RSpec macro method that sets up a cassette for an RSpec
+ example group.
## 1.1.2 (September 9, 2010)
@@ -0,0 +1,50 @@
+@fakeweb
+Feature: RSpec integration
+ In order to easily use VCR with RSpec
+ As a user of both VCR and RSpec
+ I want a simple macro that uses a cassette for an example group
+
+ Scenario: `use_vcr_cassette` macro for RSpec 2
+ Given the following files do not exist:
+ | cassettes/VCR-RSpec_integration/without_an_explicit_cassette_name.yml |
+ | cassettes/net_http_example.yml |
+ And a file named "use_vcr_cassette_spec.rb" with:
+ """
+ require 'vcr/rspec'
+
+ VCR.config do |c|
+ c.cassette_library_dir = 'cassettes'
+ c.http_stubbing_library = :fakeweb
+ c.default_cassette_options = { :record => :new_episodes }
+ end
+
+ RSpec.configure do |c|
+ c.extend VCR::RSpec::Macros
+ end
+
+ describe "VCR-RSpec integration" do
+ def make_http_request
+ Net::HTTP.get_response('example.com', '/').body
+ end
+
+ context "without an explicit cassette name" do
+ use_vcr_cassette
+
+ it 'records an http request' do
+ make_http_request.should =~ /You have reached this web page by typing.*example\.com/
+ end
+ end
+
+ context "with an explicit cassette name" do
+ use_vcr_cassette "net_http_example"
+
+ it 'records an http request' do
+ make_http_request.should =~ /You have reached this web page by typing.*example\.com/
+ end
+ end
+ end
+ """
+ When I run "rspec ./use_vcr_cassette_spec.rb"
+ Then the output should contain "2 examples, 0 failures"
+ And the file "cassettes/VCR-RSpec_integration/without_an_explicit_cassette_name.yml" should contain "You have reached this web page by typing"
+ And the file "cassettes/net_http_example.yml" should contain "You have reached this web page by typing"
@@ -247,3 +247,6 @@ def capture_response(url)
Timecop.travel(last_changed_at + day_count.to_i.days)
end
+Given /the following files do not exist:/ do |files|
+ check_file_presence(files.raw.map{|file_row| file_row[0]}, false)
+end
View
@@ -0,0 +1,35 @@
+require 'vcr'
+
+module VCR
+ module RSpec
+ module Macros
+ def use_vcr_cassette(*args)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ name = args.first || infer_cassette_name
+
+ before(:each) do
+ VCR.insert_cassette(name, options)
+ end
+
+ after(:each) do
+ VCR.eject_cassette
+ end
+ end
+
+ private
+
+ def infer_cassette_name
+ group_descriptions = []
+ klass = self
+
+ while klass.respond_to?(:metadata) && klass.metadata
+ group_descriptions << klass.metadata[:example_group][:description]
+ klass = klass.superclass
+ end
+
+ group_descriptions.compact.reverse.join('/')
+ end
+ end
+ end
+end
+
View
@@ -0,0 +1,47 @@
+require 'spec_helper'
+require 'vcr/rspec'
+
+describe VCR::RSpec::Macros do
+ extend described_class
+
+ describe '#use_vcr_cassette' do
+ def self.perform_test(context_name, expected_cassette_name, *args, &block)
+ context context_name do
+ after(:each) do
+ if @test_ejection
+ VCR.current_cassette.should be_nil
+ end
+ end
+
+ use_vcr_cassette *args
+
+ it 'ejects the cassette in an after hook' do
+ VCR.current_cassette.should be_a(VCR::Cassette)
+ @test_ejection = true
+ end
+
+ it "creates a cassette named '#{expected_cassette_name}" do
+ VCR.current_cassette.name.should == expected_cassette_name
+ end
+
+ module_eval(&block) if block
+ end
+ end
+
+ perform_test 'when called with an explicit name', 'explicit_name', 'explicit_name'
+
+ perform_test 'when called with an explicit name and some options', 'explicit_name', 'explicit_name', :match_requests_on => [:method, :host] do
+ it 'uses the provided cassette options' do
+ VCR.current_cassette.match_requests_on.should == [:method, :host]
+ end
+ end
+
+ perform_test 'when called with nothing', 'VCR::RSpec::Macros/#use_vcr_cassette/when called with nothing'
+
+ perform_test 'when called with some options', 'VCR::RSpec::Macros/#use_vcr_cassette/when called with some options', :match_requests_on => [:method, :host] do
+ it 'uses the provided cassette options' do
+ VCR.current_cassette.match_requests_on.should == [:method, :host]
+ end
+ end
+ end
+end

0 comments on commit f4cce4c

Please sign in to comment.