The Difference between Mocks and Stubs

Posted by James Mead Mon, 11 Sep 2006 02:59:00 GMT

Some recent converstations have made me realise that a lot of people are confused by the difference between mocking and stubbing. I’ve realised that I’ve only added to the confusion by calling part of the Mocha library, Stubba.

The difference between mocking and stubbing

Stubbing a method is all about replacing the method with code that returns a specified result (or perhaps raises a specified exception). Mocking a method is all about asserting that a method has been called (perhaps with particular parameters).

If you think about it, it’s difficult (or impossible?) to do mocking without stubbing – you need to return from the mocked method, so that the code under test can complete execution. So mocking libraries tend to implicitly or explicitly allow you to do stubbing.

Mocking and stubbing with Mocha

To stub a method, use the stubs method. To mock a method, use the expects method.

The difference between Mocha and Stubba

Mocha is a traditional mocking library very much in the JMock mould. Stubba is a separate part of Mocha that allows mocking and stubbing of methods on real (non-mock) classes. It works by moving the method of interest to one side, adding a new stubbed version of the method which delegates to a traditional mock object. You can use this mock object to set up stubbed return values or set up expectations of methods to be called. After the test completes the stubbed version of the method is removed and replaced by the original.

I’m thinking of ditching the name Stubba, because this part of the library is not solely concerned with stubbing. Let me know what you think.

Martin Fowler has a must-read article on why Mocks Aren’t Stubs.

Another great reference is JMocks documentation – in particular Yoga for your Unit Tests.

Read more...

Tags , , , , , , , , ,  | 3 comments

New Mocha Docs

Posted by James Mead Sun, 03 Sep 2006 18:20:00 GMT

Spurred on by Thorsten’s comment on Gluttonous’ blog and a mention of Mocha in the Top 5 Rails Stories of the Week, I’ve given the Mocha documentation a major overhaul.

Now the RDoc only shows the public API which should hopefully improve the signal-to-noise ratio and show how simple it is to use.

I’ve also had a play with CodeRay and generated syntax-highlighted examples.

Good general information on mocking…

Tags , , , , , , , , , , , , ,  | 4 comments

Mocha 0.2 released

Posted by James Mead Fri, 11 Aug 2006 11:57:00 GMT

I’ve just released a new version of the Ruby mocking and stubbing library, Mocha, that I first mentioned a while ago. Here are the release notes:

  • Small change to SetupAndTeardown#teardown_stubs suggested by Luke Redpath to allow use of Stubba with RSpec.
  • Reorganized directory structure and extracted addition of setup and teardown methods into SmartTestCase mini-library.
  • Addition of auto-verify for Mocha (but not Stubba). This means there is more significance in the choice of expects or stubs in that any expects on a mock will automatically get verified.

So instead of…

  wotsit = Mocha.new
  wotsit.expects(:thingummy).with(5).returns(10)
  doobrey = Doobrey.new(wotsit)
  doobrey.hoojamaflip
  wotsit.verify

you need to do…

  wotsit = mock()
  wotsit.expects(:thingummy).with(5).returns(10)
  doobrey = Doobrey.new(wotsit)
  doobrey.hoojamaflip
  # no need to verify

There are also shortcuts as follows…

instead of…

  wotsit = Mocha.new
  wotsit.expects(:thingummy).returns(10)
  wotsit.expects(:summat).returns(25)

you can have…

  wotsit = mock(:thingummy => 5, :summat => 25)

and instead of…

  wotsit = Mocha.new
  wotsit.stubs(:thingummy).returns(10)
  wotsit.stubs(:summat).returns(25)

you can have…

  wotsit = stub(:thingummy => 5, :summat => 25)

Posted in  | Tags , , , , , , , , ,  | 4 comments