views:

515

answers:

3

I notice that a lot of people prefer mocha over rspec's built in mocking framework. Can someone explain the advantages of mocha, or any alternative, over rspec's built in mocking framework?

A: 

I for one use mocha because I don't use rspec. I use test/unit, and test/unit doesn't have stubbing and mocking built-in.

August Lilleaas
+1  A: 

As far as I know Mocha supports Double Injections (aka Partial Mocking, which is also supported in rr), not sure that RSpec supports this feature too.

Also, for those who prefer to switch between testing frameworks, mocha is a universal solution applicable for Test/Unit, Shoulda, etc. Using RSpec mocking with all these libs will be an overkill.

Kirill Ishanov
+4  A: 

One specific feature I really like is being able to stub out all instances of a class. A lot of times I do something like the following with rspec mocks:

stub_car = mock(Car)
stub_car.stub!(:speed).and_return(100)
Car.stub!(:new).and_return(stub_car)

with mocha that becomes

Car.any_instance.stubs(:speed).returns(100)

I find the mocha version clearer and more explicit.

Pete Hodgson
any_instance is probably *the* reason to use Mocha. It's incredibly powerful and can save a lot of time.
Brian Hogan