Permalink
Browse files

require 'rspec/mocks' does not add methods to Object or ExampleGroup

- call RSpec::Mocks::setup(self) from an example to add double(),
  mock(), and stub() methods to self, and should_receive(),
  should_not_receive(), and stub() methods to Object.
- NOTE the stub() on self is an alias for double(), whereas the stub()
  on every other object is a method stub declaration.

Closes #7.
  • Loading branch information...
1 parent 092f785 commit e8b3fded41692304f9aa2f7985c88b9a619c0b9e @dchelimsky dchelimsky committed Jul 3, 2010
@@ -0,0 +1,82 @@
+Feature: configure a test frawework to use rspec-mocks
+
+ Test frameworks that want to use rspec-mocks can use
+ RSpec::Mocks::setup(self) to hook into rspec-mocks. Doing so adds the
+ following:
+
+ To the object passed to setup:
+
+ double # creates a test double
+ mock # creates a test double
+ stub # creates a test double
+
+ To every object in the system:
+
+ should_receive
+ should_not_receive
+ stub
+
+ In order to give control to the consuming framework, none of these facilities
+ are added until RSpec::Mocks::setup(self) is called. Simply requiring
+ 'rspec/mocks' is not sufficient.
+
+ NOTICE: the stub() method that is added to the object passed to setup is not
+ the same stub() method that is added to every other object.
+
+ Scenario: RSpec::Mocks::setup(object) adds double, mock, and stub methods to the submitted object
+ Given a file named "foo.rb" with:
+ """
+ require 'rspec/mocks'
+
+ class CodeExample
+ def init
+ RSpec::Mocks::setup(self)
+ end
+ end
+
+ example = CodeExample.new
+ example.init
+
+ puts example.respond_to?(:double)
+ puts example.respond_to?(:mock)
+ puts example.respond_to?(:stub)
+ """
+
+ When I run "ruby foo.rb"
+ Then the output should contain "true"
+ But the output should not contain "false"
+
+ Scenario: RSpec::Mocks::setup(anything) adds methods to Object
+ Given a file named "foo.rb" with:
+ """
+ require 'rspec/mocks'
+
+ RSpec::Mocks::setup(Object.new)
+
+ obj = Object.new
+
+ puts obj.respond_to?(:should_receive)
+ puts obj.respond_to?(:should_not_receive)
+ puts obj.respond_to?(:stub)
+ """
+
+ When I run "ruby foo.rb"
+ Then the output should contain "true"
+ But the output should not contain "false"
+
+ Scenario: require "rspec/mocks" does not add methods to Object
+ Given a file named "foo.rb" with:
+ """
+ require 'rspec/mocks'
+
+ obj = Object.new
+
+ puts obj.respond_to?(:should_receive)
+ puts obj.respond_to?(:should_not_receive)
+ puts obj.respond_to?(:stub)
+ """
+
+ When I run "ruby foo.rb"
+ Then the output should contain "false"
+ But the output should not contain "true"
+
@@ -30,7 +30,7 @@ Feature: block local expectations
end
"""
When I run "rspec ./spec/account_passing_spec.rb"
- Then I should see "1 example, 0 failures"
+ Then the output should contain "1 example, 0 failures"
Scenario: failing example
@@ -51,4 +51,4 @@ Feature: block local expectations
"""
When I run "rspec ./spec/account_failing_spec.rb"
- Then I should see "1 example, 1 failure"
+ Then the output should contain "1 example, 1 failure"
@@ -25,4 +25,4 @@ Feature: Spec and test together
end
"""
When I run "rspec ./stub_and_mocks_spec.rb -fs"
- Then I should see "received :foo with unexpected arguments"
+ Then the output should contain "received :foo with unexpected arguments"
@@ -12,7 +12,7 @@ Feature: warn when expectation is set on nil
end
"""
When I run "rspec ./example_spec.rb"
- Then I should see "An expectation of :foo was set on nil"
+ Then the output should contain "An expectation of :foo was set on nil"
Scenario: allow
Given a file named "example_spec.rb" with:
@@ -27,7 +27,7 @@ Feature: warn when expectation is set on nil
end
"""
When I run "rspec ./example_spec.rb"
- Then I should not see "An expectation"
+ Then the output should not contain "An expectation"
Scenario: allow in one example, but not on another
Given a file named "example_spec.rb" with:
@@ -46,5 +46,5 @@ Feature: warn when expectation is set on nil
end
"""
When I run "rspec ./example_spec.rb"
- Then I should see "An expectation of :bar"
- And I should not see "An expectation of :foo"
+ Then the output should contain "An expectation of :bar"
+ And the output should not contain "An expectation of :foo"
@@ -27,4 +27,4 @@ Feature: stub implementation
end
"""
When I run "rspec ./stub_implementation_spec.rb"
- Then I should see "1 example, 0 failures"
+ Then the output should contain "1 example, 0 failures"
View
@@ -1,5 +1,4 @@
require 'rspec/mocks/framework'
-require 'rspec/mocks/extensions'
require 'rspec/mocks/version'
module RSpec
@@ -176,7 +175,12 @@ module Mocks
class << self
attr_accessor :space
- def setup
+ def setup(includer)
+ require 'rspec/mocks/extensions/object'
+ require 'rspec/mocks/spec_methods'
+ (class << includer; self; end).class_eval do
+ include RSpec::Mocks::ExampleMethods
+ end
self.space ||= RSpec::Mocks::Space.new
end
@@ -1,2 +0,0 @@
-require 'rspec/mocks/extensions/object'
-require 'rspec/mocks/extensions/instance_exec'
@@ -2,10 +2,10 @@
# supports wrapping rspec's mocking functionality without invading every
# object in the system.
+require 'rspec/mocks/extensions/instance_exec'
require 'rspec/mocks/method_double'
require 'rspec/mocks/methods'
require 'rspec/mocks/argument_matchers'
-require 'rspec/mocks/spec_methods'
require 'rspec/mocks/proxy'
require 'rspec/mocks/mock'
require 'rspec/mocks/argument_expectation'
View
@@ -12,17 +12,17 @@
end
it "memoizes the space" do
- RSpec::Mocks::setup
+ RSpec::Mocks::setup(Object.new)
space = RSpec::Mocks::space
- RSpec::Mocks::setup
+ RSpec::Mocks::setup(Object.new)
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::setup(Object.new)
RSpec::Mocks::space.should_not be_nil
end
end

0 comments on commit e8b3fde

Please sign in to comment.