Skip to content

Commit

Permalink
Pass the instance to should_receive matchers on any instance too.
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 Jul 7, 2013
1 parent 6287f7a commit a37e021
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/rspec/mocks/any_instance/expectation_chain.rb
Expand Up @@ -25,6 +25,11 @@ class PositiveExpectationChain < ExpectationChain
def create_message_expectation_on(instance)
proxy = ::RSpec::Mocks.proxy_for(instance)
expected_from = IGNORED_BACKTRACE_LINE
if @expectation_args.last.is_a? Hash
@expectation_args.last[:is_any_instance_expectation] = true
else
@expectation_args << {:is_any_instance_expectation => true}
end
proxy.add_message_expectation(expected_from, *@expectation_args, &@expectation_block)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/mocks/any_instance/recorder.rb
Expand Up @@ -173,7 +173,7 @@ def observe!(method_name, ignore_instance=false)
@klass.__send__(:define_method, method_name) do |*args, &blk|
klass = ::RSpec::Mocks.method_handle_for(self, method_name).owner
::RSpec::Mocks.any_instance_recorder_for(klass).playback!(self, method_name)
if !ignore_instance && ::RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs
if ::RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs
args.unshift(self)
end
self.__send__(method_name, *args, &blk)
Expand Down
5 changes: 5 additions & 0 deletions lib/rspec/mocks/message_expectation.rb
Expand Up @@ -24,6 +24,7 @@ def initialize(error_generator, expectation_ordering, expected_from, method_doub
@args_to_yield = []
@failed_fast = nil
@eval_context = nil
@is_any_instance_expectation = opts[:is_any_instance_expectation]

@implementation = Implementation.new
self.inner_implementation_action = implementation_block
Expand Down Expand Up @@ -168,6 +169,10 @@ def and_yield(*args, &block)

# @private
def matches?(message, *args)
args = args.dup
if @is_any_instance_expectation && ::RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs
args.shift
end
@message == message && @argument_list_matcher.args_match?(*args)
end

Expand Down
11 changes: 9 additions & 2 deletions spec/rspec/mocks/any_instance_spec.rb
Expand Up @@ -858,10 +858,10 @@ def foo; end
instance.bees(:sup)
end

it "does not pass the instance" do
it "passes the instance" do
klass = Struct.new(:science)
instance = klass.new
klass.any_instance.should_receive(:bees).with(:sup) { |*args| expect(args.first).to eq(:sup) }
klass.any_instance.should_receive(:bees).with(:sup) { |*args| expect(args.first).to eq(instance) }
instance.bees(:sup)
end
end
Expand All @@ -884,6 +884,13 @@ def foo; end
klass.any_instance.stub(:bees) { |*args| expect(args).to be_empty }
instance.bees
end

it "gets data from with correctly" do
klass = Struct.new(:science)
instance = klass.new
klass.any_instance.should_receive(:bees).with(:faces)
instance.bees(:faces)
end
end
end
end
Expand Down

0 comments on commit a37e021

Please sign in to comment.