Skip to content
This repository
Browse code

Support multiple chains that start with the same message

- forward ported from rspec-1
- Closes #9.
  • Loading branch information...
commit 76073456b7b9d97a818554f736eda2dd96adccac 1 parent 1b63ca2
David Chelimsky authored August 09, 2010
11  lib/rspec/mocks/methods.rb
@@ -37,9 +37,14 @@ def unstub(sym)
37 37
       def stub_chain(*chain)
38 38
         methods = chain.join('.').split('.')
39 39
         if methods.length > 1
40  
-          next_in_chain = Object.new
41  
-          stub(methods.shift) { next_in_chain }
42  
-          next_in_chain.stub_chain(*methods)
  40
+          if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, methods[0].to_sym)
  41
+            methods.shift
  42
+            matching_stub.__send__(:invoke, [], nil).stub_chain(*methods)
  43
+          else
  44
+            next_in_chain = Object.new
  45
+            stub(methods.shift) { next_in_chain }
  46
+            next_in_chain.stub_chain(*methods)
  47
+          end
43 48
         else
44 49
           stub(methods.shift)
45 50
         end
8  spec/rspec/mocks/stub_chain_spec.rb
@@ -34,6 +34,14 @@ module Mocks
34 34
         @subject.stub_chain("msg1.msg2.msg3").and_return(:return_value)
35 35
         @subject.msg1.msg2.msg3.should equal(:return_value)
36 36
       end
  37
+
  38
+      it "returns expected value from two chains with shared messages at the beginning" do
  39
+        @subject.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:first)
  40
+        @subject.stub_chain(:msg1, :msg2, :msg3, :msg5).and_return(:second)
  41
+
  42
+        @subject.msg1.msg2.msg3.msg4.should equal(:first)
  43
+        @subject.msg1.msg2.msg3.msg5.should equal(:second)
  44
+      end
37 45
     end
38 46
   end
39 47
 end

0 notes on commit 7607345

Please sign in to comment.
Something went wrong with that request. Please try again.