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?
views:
515answers:
3
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
2009-09-11 08:20:20
+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
2009-09-11 14:05:00
+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
2009-09-13 06:01:15
any_instance is probably *the* reason to use Mocha. It's incredibly powerful and can save a lot of time.
Brian Hogan
2009-09-13 06:18:17