Permalink
Browse files

Fixed regression that broke hash format for stub_chain

- obj.stub_chain(:a, :b => :c)
  • Loading branch information...
1 parent 5f24e7d commit 2c23b86cc5aaa99557df456054765e95aa1a0d5a @dchelimsky dchelimsky committed Sep 11, 2010
Showing with 47 additions and 8 deletions.
  1. +19 −8 lib/rspec/mocks/methods.rb
  2. +28 −0 spec/rspec/mocks/stub_chain_spec.rb
@@ -35,18 +35,18 @@ def unstub(sym)
#
# Article.stub_chain("recent.published") { [Article.new] }
def stub_chain(*chain, &blk)
- methods = chain.join('.').split('.')
- if methods.length > 1
- if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, methods[0].to_sym)
- methods.shift
- matching_stub.invoke.stub_chain(*methods)
+ chain, blk = format_chain(*chain, &blk)
+ if chain.length > 1
+ if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym)
+ chain.shift
+ matching_stub.invoke.stub_chain(*chain)
else
next_in_chain = Object.new
- stub(methods.shift) { next_in_chain }
- next_in_chain.stub_chain(*methods, &blk)
+ stub(chain.shift) { next_in_chain }
+ next_in_chain.stub_chain(*chain, &blk)
end
else
- stub(methods.shift, &blk)
+ stub(chain.shift, &blk)
end
end
@@ -79,6 +79,17 @@ def __mock_proxy
@mock_proxy ||= Proxy.new(self)
end
end
+
+ def format_chain(*chain, &blk)
+ if Hash === chain.last
+ hash = chain.pop
+ hash.each do |k,v|
+ chain << k
+ blk = lambda { v }
+ end
+ end
+ return chain.join('.').split('.'), blk
+ end
end
end
end
@@ -22,6 +22,13 @@ module Mocks
@subject.msg1.should equal(:return_value)
end
end
+
+ context "using a hash" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain(:msg1 => :return_value)
+ @subject.msg1.should equal(:return_value)
+ end
+ end
end
context "with two methods in chain" do
@@ -38,6 +45,13 @@ module Mocks
@subject.msg1.msg2.should equal(:return_value)
end
end
+
+ context "using a hash" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain(:msg1, :msg2 => :return_value)
+ @subject.msg1.msg2.should equal(:return_value)
+ end
+ end
end
context "with four methods in chain" do
@@ -54,6 +68,20 @@ module Mocks
@subject.msg1.msg2.msg3.msg4.should equal(:return_value)
end
end
+
+ context "using a hash" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain(:msg1, :msg2, :msg3, :msg4 => :return_value)
+ @subject.msg1.msg2.msg3.msg4.should equal(:return_value)
+ end
+ end
+
+ context "using a hash with a string key" do
+ it "returns the value of the key/value pair" do
+ @subject.stub_chain("msg1.msg2.msg3.msg4" => :return_value)
+ @subject.msg1.msg2.msg3.msg4.should equal(:return_value)
+ end
+ end
end
it "returns expected value from chaining four method calls" do

0 comments on commit 2c23b86

Please sign in to comment.