Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix regression that broke stub_chain with block format

- obj.stub_chain(:a,:b) { :c }
  • Loading branch information...
commit 5f24e7d332f5a274c986b283f0aff3ee5f925093 1 parent 537838f
@dchelimsky dchelimsky authored
Showing with 48 additions and 9 deletions.
  1. +3 −3 lib/rspec/mocks/methods.rb
  2. +45 −6 spec/rspec/mocks/stub_chain_spec.rb
View
6 lib/rspec/mocks/methods.rb
@@ -34,7 +34,7 @@ def unstub(sym)
# == Examples
#
# Article.stub_chain("recent.published") { [Article.new] }
- def stub_chain(*chain)
+ 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)
@@ -43,10 +43,10 @@ def stub_chain(*chain)
else
next_in_chain = Object.new
stub(methods.shift) { next_in_chain }
- next_in_chain.stub_chain(*methods)
+ next_in_chain.stub_chain(*methods, &blk)
end
else
- stub(methods.shift)
+ stub(methods.shift, &blk)
end
end
View
51 spec/rspec/mocks/stub_chain_spec.rb
@@ -7,14 +7,53 @@ module Mocks
@subject = Object.new
end
- it "returns expected value from chaining only one method call" do
- @subject.stub_chain(:msg1).and_return(:return_value)
- @subject.msg1.should equal(:return_value)
+
+ context "with one method in chain" do
+ context "using and_return" do
+ it "returns expected value from chaining only one method call" do
+ @subject.stub_chain(:msg1).and_return(:return_value)
+ @subject.msg1.should equal(:return_value)
+ end
+ end
+
+ context "using a block" do
+ it "returns the correct value" do
+ @subject.stub_chain(:msg1) { :return_value }
+ @subject.msg1.should equal(:return_value)
+ end
+ end
+ end
+
+ context "with two methods in chain" do
+ context "using and_return" do
+ it "returns expected value from chaining two method calls" do
+ @subject.stub_chain(:msg1, :msg2).and_return(:return_value)
+ @subject.msg1.msg2.should equal(:return_value)
+ end
+ end
+
+ context "using a block" do
+ it "returns the correct value" do
+ @subject.stub_chain(:msg1, :msg2) { :return_value }
+ @subject.msg1.msg2.should equal(:return_value)
+ end
+ end
end
- it "returns expected value from chaining two method calls" do
- @subject.stub_chain(:msg1, :msg2).and_return(:return_value)
- @subject.msg1.msg2.should equal(:return_value)
+ context "with four methods in chain" do
+ context "using and_return" do
+ it "returns expected value from chaining two method calls" do
+ @subject.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:return_value)
+ @subject.msg1.msg2.msg3.msg4.should equal(:return_value)
+ end
+ end
+
+ context "using a block" do
+ it "returns the correct value" 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
Please sign in to comment.
Something went wrong with that request. Please try again.