10.10 Creating Strongly Typed Mocks with Rhino.Mocks

You need to create mock objects to let you decouple your work from external resources, but you’re running into some problems. What do you do if you need to create mock objects for classes with no default constructors, or classes with parameterized constructors? What about generic objects? What do you do if you need to explicitly check the order of methods called on mocked objects? And wouldn’t it be nice if you could use strongly typed mocked objects, so you could access methods and properties directly when writing tests, instead of passing in strings of names (which, by the way, aren’t checked at compile-time, so you won’t know you’ve made a mistake until you run your tests)?

Rhino.Mocks, by Oren Eini, offers an attractive alternative to the NMock and NMock 2.0 libraries. Rhino.Mocks’s model creates strongly typed mock objects, so you’ll get full IntelliSense support as you’re coding. Also, the IDE and compiler will point out errors as you work, so you won’t have to wait until runtime to find you’ve mistyped a string representing a method name.

Rhino.Mocks enables you to mock up classes as well as interfaces. It also supports calling parameterized constructors. Rhino.Mocks has a very rich set of constraints you can place against expectations, and you can specify that recorded expectations must occur in a specific order. Rhino.Mocks supports generic classes and working with generic methods, which is of great importance to developers ...

Get Windows Developer Power Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.