Skip to content

Commit

Permalink
Pass the receiving instance to any instance stubs.
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
  • Loading branch information
Sam Phippen committed Aug 1, 2013
1 parent 4a01273 commit ebd1cda
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Bug Fixes:
* Fix regression in 2.14 that made `stub!` (with an implicit receiver)
return a test double rather than stub a method (Myron Marston).

Enhancements:

* Yield the receiver to `any_instance` implementation blocks (Sam Phippen).

### 2.14.1 / 2013-07-07
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.14.0...v2.14.1)

Expand Down
7 changes: 6 additions & 1 deletion lib/rspec/mocks/any_instance/expectation_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class PositiveExpectationChain < ExpectationChain
def create_message_expectation_on(instance)
proxy = ::RSpec::Mocks.proxy_for(instance)
expected_from = IGNORED_BACKTRACE_LINE
proxy.add_message_expectation(expected_from, *@expectation_args, &@expectation_block)
me = proxy.add_message_expectation(expected_from, *@expectation_args, &@expectation_block)
if RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks?
me.and_yield_receiver_to_implementation
end

me
end

def invocation_order
Expand Down
8 changes: 7 additions & 1 deletion lib/rspec/mocks/any_instance/stub_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def expectation_fulfilled?
def create_message_expectation_on(instance)
proxy = ::RSpec::Mocks.proxy_for(instance)
expected_from = IGNORED_BACKTRACE_LINE
proxy.add_stub(expected_from, *@expectation_args, &@expectation_block)
stub = proxy.add_stub(expected_from, *@expectation_args, &@expectation_block)

if RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks?
stub.and_yield_receiver_to_implementation
end

stub
end

def invocation_order
Expand Down
13 changes: 13 additions & 0 deletions lib/rspec/mocks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ module RSpec
module Mocks
# Provides configuration options for rspec-mocks.
class Configuration

def initialize
@yield_receiver_to_any_instance_implementation_blocks = true
end

def yield_receiver_to_any_instance_implementation_blocks?
@yield_receiver_to_any_instance_implementation_blocks
end

def yield_receiver_to_any_instance_implementation_blocks=(arg)
@yield_receiver_to_any_instance_implementation_blocks = arg
end

# Adds `stub` and `should_receive` to the given
# modules or classes. This is usually only necessary
# if you application uses some proxy classes that
Expand Down
17 changes: 17 additions & 0 deletions lib/rspec/mocks/message_expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class MessageExpectation
# @private
attr_accessor :error_generator, :implementation
attr_reader :message
attr_reader :orig_object
attr_writer :expected_received_count, :expected_from, :argument_list_matcher
protected :expected_received_count=, :expected_from=, :error_generator, :error_generator=, :implementation=

Expand All @@ -15,6 +16,7 @@ def initialize(error_generator, expectation_ordering, expected_from, method_doub
@error_generator.opts = opts
@expected_from = expected_from
@method_double = method_double
@orig_object = @method_double.object
@message = @method_double.method_name
@actual_received_count = 0
@expected_received_count = expected_received_count
Expand All @@ -24,6 +26,7 @@ def initialize(error_generator, expectation_ordering, expected_from, method_doub
@args_to_yield = []
@failed_fast = nil
@eval_context = nil
@yield_receiver_to_implementation_block = false

@implementation = Implementation.new
self.inner_implementation_action = implementation_block
Expand Down Expand Up @@ -87,6 +90,15 @@ def and_return(*values, &implementation)
nil
end

def and_yield_receiver_to_implementation
@yield_receiver_to_implementation_block = true
self
end

def yield_receiver_to_implementation_block?
@yield_receiver_to_implementation_block
end

# Tells the object to delegate to the original unmodified method
# when it receives the message.
#
Expand All @@ -103,6 +115,7 @@ def and_call_original
@error_generator.raise_only_valid_on_a_partial_mock(:and_call_original)
else
@implementation = AndCallOriginalImplementation.new(@method_double.original_method)
@yield_receiver_to_implementation_block = false
end
end

Expand Down Expand Up @@ -171,6 +184,10 @@ def matches?(message, *args)

# @private
def invoke(parent_stub, *args, &block)
if yield_receiver_to_implementation_block?
args.unshift(orig_object)
end

if negative? || ((@exactly || @at_most) && (@actual_received_count == @expected_received_count))
@actual_received_count += 1
@failed_fast = true
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/mocks/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def replay_received_message_on(expectation)
expectation.invoke(nil)
end
end

end

# @private
Expand Down
72 changes: 72 additions & 0 deletions spec/rspec/mocks/any_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,78 @@ def foo; end
end
end

context "passing the receiver to the implementation block" do
context "when configured to pass the instance" do
include_context 'with isolated configuration'
before(:each) do
RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = true
end

describe "an any instance stub" do
it "passes the instance as the first arg of the implementation block" do
instance = klass.new

expect { |b|
klass.any_instance.should_receive(:bees).with(:sup, &b)
instance.bees(:sup)
}.to yield_with_args(instance, :sup)
end

it "does not pass the instance to and_call_original" do
klass = Class.new do
def call(*args)
args.first
end
end
klass.any_instance.should_receive(:call).and_call_original
instance = klass.new
expect(instance.call(:bees)).to be :bees
end
end

describe "an any instance expectation" do
it "doesn't effect with" do
instance = klass.new
klass.any_instance.should_receive(:bees).with(:sup)
instance.bees(:sup)
end

it "passes the instance as the first arg of the implementation block" do
instance = klass.new

expect { |b|
klass.any_instance.should_receive(:bees).with(:sup, &b)
instance.bees(:sup)
}.to yield_with_args(instance, :sup)
end
end
end

context "when configured not to pass the instance" do
include_context 'with isolated configuration'
before(:each) do
RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false
end

describe "an any instance stub" do
it "does not pass the instance to the implementation block" do
instance = klass.new

expect { |b|
klass.any_instance.should_receive(:bees).with(:sup, &b)
instance.bees(:sup)
}.to yield_with_args(:sup)
end

it "does not cause with to fail when the instance is passed" do
instance = klass.new
klass.any_instance.should_receive(:bees).with(:faces)
instance.bees(:faces)
end
end
end
end

context 'when used in conjunction with a `dup`' do
it "doesn't cause an infinite loop" do
pending "This intermittently fails on JRuby" if RUBY_PLATFORM == 'java'
Expand Down
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ def reset(object)
end
end


shared_context "with isolated configuration" do
orig_configuration = nil
before do
orig_configuration = RSpec::Mocks.configuration
RSpec::Mocks.instance_variable_set(:@configuration, RSpec::Mocks::Configuration.new)
end

after do
RSpec::Mocks.instance_variable_set(:@configuration, orig_configuration)
end
end

0 comments on commit ebd1cda

Please sign in to comment.