I am not at RailsConf but I have been following the tweets of my coworkers, so I heard about RSpec‘s new stub_chain method. Since I prefer Mocha’s syntax to that of the built-in RSpec stubbing framework, I created a monkey patch to add stub_chain to Mocha:

 1module StubChainMocha
 2  module Object
 3    def stub_chain(*methods)
 4      while methods.length > 1 do
 5        stubs(methods.shift).returns(self)
 6      end
 7      stubs(methods.shift)
 8    end
 9  end
10end
11
12Object.send(:include, StubChainMocha::Object)

Copy that into spec/stub_chain_mocha.rb and then require it from spec_helper.rb.

blog comments powered by Disqus