Injecting mocks (the Mocha way)
Posted by James Mead Fri, 01 Sep 2006 06:26:00 GMT
I thought I’d compare and contrast the way Gluttonous was injecting mocks and how you can do it with Mocha.
Gluttonous’ way
def test_process_exit
delegate_methods_to_mock!(RailsFCGIHandler, :close_connection) do
fcgi = flexmock()
fcgi.should_receive(:close_connection)
@handler.mock = fcgi
@handler.stubs(:when_ready).returns(:exit)
@handler.process!
end
endUsing Mocha
def test_process_exit
@handler.expects(:close_connection)
@handler.stubs(:when_ready).returns(:exit)
@handler.process!
endI think it’s a bit more readable and you don’t need the block construction which starts becoming a nuisance when you need to stub methods on multiple classes.

Oh seriously? Damn it, now I have to try and push mocha in there instead of flexmock. You’re just giving me all sorts of problems this week James.
Yup. Sorry – I really need to write up some better documentation :-)
I’ll try and write a quickstart article…
no bad James.
no bad James.