Skip to content
Browse files

expose APIs to tell RSpec::Mocks to setup, verify, and teardown

- this eliminates the $rspec_mocks global
- allows other tools like Cucumber to use RSpec::Mocks through a
  published API

Closes #6.
  • Loading branch information...
1 parent b3a5d50 commit db925f8acb68d8a721618284152ff613ab6584a2 @dchelimsky dchelimsky committed
Showing with 68 additions and 2 deletions.
  1. +15 −0 lib/rspec/mocks.rb
  2. +1 −1 lib/rspec/mocks/method_double.rb
  3. +1 −1 lib/rspec/mocks/proxy.rb
  4. +51 −0 spec/rspec/mocks_spec.rb
View
15 lib/rspec/mocks.rb
@@ -173,5 +173,20 @@ module RSpec
# * Test Double Patterns: http://xunitpatterns.com/Test%20Double%20Patterns.html
# * Mocks aren't stubs: http://www.martinfowler.com/articles/mocksArentStubs.html
module Mocks
+ class << self
+ attr_accessor :space
+
+ def setup
+ self.space ||= RSpec::Mocks::Space.new
+ end
+
+ def verify
+ space.verify_all
+ end
+
+ def teardown
+ space.reset_all
+ end
+ end
end
end
View
2 lib/rspec/mocks/method_double.rb
@@ -55,7 +55,7 @@ def object_responds_to?(method_name)
end
def configure_method
- $rspec_mocks.add(@object) if $rspec_mocks
+ RSpec::Mocks::space.add(@object) if RSpec::Mocks::space
warn_if_nil_class
unless @stashed
stash_original_method
View
2 lib/rspec/mocks/proxy.rb
@@ -15,7 +15,7 @@ def allow_message_expectations_on_nil
# ensure nil.rspec_verify is called even if an expectation is not set in the example
# otherwise the allowance would effect subsequent examples
- $rspec_mocks.add(nil) unless $rspec_mocks.nil?
+ RSpec::Mocks::space.add(nil) unless RSpec::Mocks::space.nil?
end
def allow_message_expectations_on_nil?
View
51 spec/rspec/mocks_spec.rb
@@ -0,0 +1,51 @@
+require "spec_helper"
+
+describe RSpec::Mocks do
+ describe "::setup" do
+ context "with an existing Mock::Space" do
+ before do
+ @orig_space = RSpec::Mocks::space
+ end
+
+ after do
+ RSpec::Mocks::space = @orig_space
+ end
+
+ it "memoizes the space" do
+ RSpec::Mocks::setup
+ space = RSpec::Mocks::space
+ RSpec::Mocks::setup
+ RSpec::Mocks::space.should equal(space)
+ end
+ end
+
+ context "with no pre-existing Mock::Space" do
+ it "initializes a Mock::Space" do
+ RSpec::Mocks::space = nil
+ RSpec::Mocks::setup
+ RSpec::Mocks::space.should_not be_nil
+ end
+ end
+ end
+
+ describe "::verify" do
+ it "delegates to the space" do
+ foo = double
+ foo.should_receive(:bar)
+ expect do
+ RSpec::Mocks::verify
+ end.to raise_error(RSpec::Mocks::MockExpectationError)
+ end
+ end
+
+ describe "::teardown" do
+ it "delegates to the space" do
+ foo = double
+ foo.should_receive(:bar)
+ RSpec::Mocks::teardown
+ expect do
+ foo.bar
+ end.to raise_error(/received unexpected message/)
+ end
+ end
+end

0 comments on commit db925f8

Please sign in to comment.
Something went wrong with that request. Please try again.