Heads: Flexmock; Tails: Mocha

written by jared on August 13th, 2007 @ 05:35 PM

So I’ve been wavering for the past couple of days on which Mocking/Stubbing libraries to embrace for my Ruby coding. The Rails core code uses Flexmock, which is a pretty strong endorsement. The OpenID Authentication plugin by David Heinemeier Hansson uses Mocha. The community does tend to look to DHH for cues as to where to follow.

When I saw that the charity Testing Workshop at Ruby Hoedown was going to include information on mocking and stubbing, I became excited. Here was a chance to hear what some industry heavyweights thought about Flexmock vs. Mocha. Bruce Tate managed to discuss Mocking and Stubbing without explicitly naming a preference.

Then Chad & Marcel took the stage with their Test Driven Development approach to recreating Luigi Poker in Ruby. When the mocking question came up, Marcel simply replied (and I’m paraphrasing here), “Jim Weirich write Flexmock. He wrote Rake. He wrote RubyGems. We trust Jim.”

Functionally, both libraries support the same operations. It comes down to a question of syntax. But since I’ve come from a “seat of the pants” PHP background, I don’t have extensive mocking experience, so I don’t already have a preferred mocking syntax.

Maybe I need to sidestep the question all together. If I switch from Test::Unit to rspec, I can just use rspec’s built in mocking and stubbing libraries.

Related reading: Viget Labs “Mockfight!”