In the previous chapter I outlined some of the problems
associated with writing Mocks and Stubs manually. In this chapter we’ll see
some very elegant solutions for these problems in the form of a Mock Object
framework – a reusable library that can be used to create and configure
Stubs and Mock Objects at runtime – these are usually referred to as Dynamic
MocksandDynamic Stubs.
We’ll start this chapter off by understanding what Mock
Frameworks are and what they can do for us. We’ll then take a closer look into
one specific Mocking framework known as RhinoMocks. We’ll see how we can
use it to test various things, using it to create stubs, mocks and other
interesting things.
Later, we’ll review and contrast Rhino.Mocks to other Mocking
frameworks currently available to .NET developers, and finish up by covering
things to watch out for when using Mocking frameworks in your tests. It’s going
to be an interesting ride, but we have to start at the beginning: What are Mock
frameworks?
A Mock Object Framework is a set of programmable APIs that allow
creating Mock and Stub Objects in an easier fashion. Mock Frameworks save the
developer from the need to write repetitive code to test or simulate object
interactions.
This definition may sound a bit bland, but it needs to be very
generic in order to describe the ideas behind the various Mock frameworks out
there.
Mock frameworks exist for most languages that have a Unit Testing framework associated with them. C++ has Mockpp and several other frameworks, for example. Java has JMock, EasyMock and several other mocking frameworks, and so does .NET, including NMock, Typemock Isolator, Moq and RhinoMocks