Permalink
Browse files

Support multiple chains that start with the same message

- forward ported from rspec-1
- Closes #9.
  • Loading branch information...
1 parent 1b63ca2 commit 76073456b7b9d97a818554f736eda2dd96adccac @dchelimsky dchelimsky committed Aug 9, 2010
Showing with 16 additions and 3 deletions.
  1. +8 −3 lib/rspec/mocks/methods.rb
  2. +8 −0 spec/rspec/mocks/stub_chain_spec.rb
@@ -37,9 +37,14 @@ def unstub(sym)
def stub_chain(*chain)
methods = chain.join('.').split('.')
if methods.length > 1
- next_in_chain = Object.new
- stub(methods.shift) { next_in_chain }
- next_in_chain.stub_chain(*methods)
+ if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, methods[0].to_sym)
+ methods.shift
+ matching_stub.__send__(:invoke, [], nil).stub_chain(*methods)
+ else
+ next_in_chain = Object.new
+ stub(methods.shift) { next_in_chain }
+ next_in_chain.stub_chain(*methods)
+ end
else
stub(methods.shift)
end
@@ -34,6 +34,14 @@ module Mocks
@subject.stub_chain("msg1.msg2.msg3").and_return(:return_value)
@subject.msg1.msg2.msg3.should equal(:return_value)
end
+
+ it "returns expected value from two chains with shared messages at the beginning" do
+ @subject.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:first)
+ @subject.stub_chain(:msg1, :msg2, :msg3, :msg5).and_return(:second)
+
+ @subject.msg1.msg2.msg3.msg4.should equal(:first)
+ @subject.msg1.msg2.msg3.msg5.should equal(:second)
+ end
end
end
end

0 comments on commit 7607345

Please sign in to comment.