MOCKING FRAMEWORKS

Chapter 2 discussed the basics of mocking objects, and throughout this book we have been using the Moq framework because of its simple syntax. Moq is becoming a popular mocking framework, but it is not the only framework available. Other frameworks have features that allow you to mock objects differently.

Moq uses a declarative syntax for mocking objects, which is different from the record/playback method of mocking that many mocking frameworks support. Many of these frameworks are moving away from the record/playback method in favor of an arrange, act, and assert (AAA) method because of its ease of use.

Rhino Mocks

Rhino Mocks is a free mocking framework created by Orin Eini, who is also known for his work on the nHibernate and Castle projects. Rhino Mocks is popular because of its ease of use and list of power features. Although the syntax is not as clean as Moq's, it is a worthwhile tool to check out. Rhino Mocks supports two different styles of mocking objects: the record/playback method and the arrange, act, and assert syntax. Record/playback is now considered to be the older way of mocking objects, but it makes sense to consider the differences.

The following example uses the AAA style of mocking objects, which is similar to mocking with Moq, as you learned in Chapter 2. The basic Rhino Mocks syntax looks like this:

images
MockRepository mocks = new MockRepository(); ...

Get Professional Test-Driven Development with C#: Developing Real World Applications with TDD 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.