Tuesday, October 30, 2007

using Rhino.Mocks

As you might have realized from reading this blog, I'm a big fan of Test Driven Development (TDD). A core part of writing good tests is the use of mock objects. Usually these are instances of objects that implement the the interfaces that the class under test has dependencies on. When I started doing TDD I used to write my own mock objects and it took me a while to realize the power of a good mock object framework. When I first discovered NMock it was a major revelation and took the ease and power of my tests to a new level. NMock has a real limitation though; its use of string literals to describe methods when you set your expectations. Any framework that uses string literals to reference types or members looses most of the advantages of working with a statically typed language.

Recently there's been quite a buzz about a new mocking framework: Rhino Mocks. Its main advantage is that it uses real invocations of the mock object's methods to set up expectations for your tests. Here's a little example:




It's a very natural style of writing expectations because they mirror what the class under test should be doing. You can also refactor to your heart's content safe in the knowledge that your tests will pick up any symbol name changes.

The guy who wrote Rhino Mocks, Oren Eini AKA Ayende Rahien is a bit of a phenomena. Subscribing to the feed of his blog is like drinking from the proverbial fire-hose; eight posts last Saturday alone. He's also an active contributor to the Castle Project which I'm very interested in at the moment. Respect!

No comments: