Skip to content

Commit

Permalink
Support multiple chains that start with the same message
Browse files Browse the repository at this point in the history
- forward ported from rspec-1
- Closes #9.
  • Loading branch information
dchelimsky committed Aug 9, 2010
1 parent 1b63ca2 commit 7607345
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/rspec/mocks/methods.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/mocks/stub_chain_spec.rb
Expand Up @@ -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.