Mocking and stubbing in Ruby
Over the last few months, I’ve been working on Mocha which is a Ruby mocking library with a syntax like that of JMock and SchMock. And I’ve finally got round to putting it up on RubyForge.
Mocha comes in three parts:
- Mocha – traditional mock objects with expectations and verification
- Stubba – allows mocking and stubbing of methods on real (non-mock) classes
- AutoMocha – magically provides mocks in the place of undefined classes
Mocha and Stubba have been created by amalgamating a number of techniques developed by me and my Reevoo colleagues (Ben, Chris and Paul) into a common syntax. Both Mocha and Stubba are in use on real-world Rails projects.
AutoMocha is more experimental and is at an earlier stage of development. It’s an attempt to make it easier to write true unit tests (i.e. tests with no external dependencies).
You can find examples in the RDoc README and in the acceptance tests.