Permalink
Browse files

Clarify reformatting of chain in stub_chain

  • Loading branch information...
1 parent 2707f78 commit 6e6d9035525bf0dfa8b5f44952a4874eb92a8016 @dchelimsky dchelimsky committed Aug 1, 2010
Showing with 15 additions and 4 deletions.
  1. +15 −4 lib/rspec/mocks/methods.rb
View
19 lib/rspec/mocks/methods.rb
@@ -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

0 comments on commit 6e6d903

Please sign in to comment.