Thursday, August 20, 2009

NMock 2 vs Rhino Mocks

I am writing unit tests for legacy code. A former programmer started using NMock 2. The first problem I run into was abstract classes. NMock 2 can’t mock abstract classes. So I moved to Rhino Mocks.

The basic ideas in both mock frameworks are the same. Here is a comparison chart:

Feature

NMock 2

NMock logo

Rhino Mocks

Rhino Mocks logo

Mock interfaces + +
Create stubs + +
Mock abstract classes - + (Partial Mocks)
Mock (implemented) classes + +
Strict replay + +
Loose replay - +
Define expectations + +
Ordered expectations + +
Record-Playback - (implicit) +
Expectation with method parameters - (method as string) +
Generic framework methods + +
Record-Playback syntax (with using block) - +
Clear existing expectations from repository + -
Documentation sloppy detailed
API documentation none detailed
Intellisense - +

Replay modes

  • Strict replay: Recorded methods are accepted as valid, any other method calls cause an exception.
  • Loose replay: Unexpected method calls return null or zero, no exception.
  • Partial mocks: Mocks only the requested methods.

Record-playback

Every single mocking framework uses this method. First you define your expectations (record) and execute them (playback) after that. NMock 2 covers it from the users. Rhino Mocks uses it explicitly.

Expectation with method parameters

In case of NMock 2 you define an expectation as Expect.Once.On(aMock).Method( “MethodName” )… You do it in Rhino Mocks as Expect.Call( dependency.MethodName() )… It means that NMock 2 won’t detect code changes and will cause runtime exceptions; Rhino Mocks will cause compile time exceptions.

Record-Playback syntax

Rhino Mocks supports using(mocks.Record()){…} using(mocks.Playback()){…} syntax. It makes possible to separate expectations from test code execution, making your test cases much more structured and neat.

Clear existing expectations from repository

It sounds a good idea at first. You can erase all the expectations from the current mock repository. It can be a nightmare in practice. One of the main goals of unit testing is separating test cases from each other. When you have to clear the mock repository to be able to define new mock and expectations, you can be in big trouble. Please avoid it!

Documentation

NMock 2 has just tutorials and cheat sheets. Even the downloaded code has no XML comments. It can make your work really hard when you have to face complicated SUT (System Under Test) code. Rhino Mocks has excellent documentation with a lot of examples.

Conclusion

If you are a newbie to unit testing and mocking you can start with NMock 2. It’s easy to start with, understand and use. If you have to work with legacy code, on big project or any serious, Rhino Mocks is a must.

Link: Rhino Mocks
Link: NMock 2

1 comment:

  1. Please take a look at the NMock2 project site at http://sourceforge.net/projects/nmock2.
    There, you can find the current version of NMock2 that is based on NMock V 2 that provides:
    - mocking of virtual/abstract classes
    - non-strict mocks (in next version)
    - basic documentation
    - Intellisense with the R# plugin

    and a lot of other improvements.

    Cheers,
    Urs

    ReplyDelete