Les Hill github twitter facebook linked in archives
Posted May 05, 2009

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:

 1 module 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
10 end
11 
12 Object.send(:include, StubChainMocha::Object)

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

Thanks to Tom Preston-Werner for the CSS layout, Webby for the blog renderer, and GitHub Pages for the blog hosting.