Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Clarify reformatting of chain in stub_chain

  • Loading branch information...
commit 6e6d9035525bf0dfa8b5f44952a4874eb92a8016 1 parent 2707f78
David Chelimsky dchelimsky authored
Showing with 15 additions and 4 deletions.
  1. +15 −4 lib/rspec/mocks/methods.rb
19 lib/rspec/mocks/methods.rb
View
@@ -24,13 +24,24 @@ def unstub(sym)
alias_method :stub!, :stub
alias_method :unstub!, :unstub
- def stub_chain(*methods)
- if methods = methods.join('.').split('.') and methods.length > 1
+ # :call-seq:
+ # double.stub_chain("foo.bar") { :baz }
+ # double.stub_chain(:foo, :bar) { :baz }
+ #
+ # Stubs a chain of methods. Especially useful with fluent and/or
+ # composable interfaces.
+ #
+ # == Examples
+ #
+ # Article.stub_chain("recent.published") { [Article.new] }
+ def stub_chain(*chain)
+ methods = chain.join('.').split('.')
+ if methods.length > 1
next_in_chain = Object.new
- stub!(methods.shift) {next_in_chain}
+ stub(methods.shift) { next_in_chain }
next_in_chain.stub_chain(*methods)
else
- stub!(methods.shift)
+ stub(methods.shift)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.