Fluent Rhino Mocks

I was recently introduced to the fluent interface that the Rhino Mocks library exposes. I wasn’t sure that it was as easy to read as the Record/Playback syntax I was use to using. I persisted with the ‘new’ syntax to see if I could see any benefit of using the fluent interface.

I now find that using the fluent interface helps on a number of fronts.

  • Eliminates many potential mistakes buy using method chaining.
  • Helps me keep to one Expectation or Assert per test.
  • Makes my code more like an English sentence and therefore easier to read.
  • Means that my code is smaller.
  • Allows me to use ExpectingInSameOrder – although I haven’t found a really good use for this yet.

I have to say I wasn’t instantly sold on the syntax but the more I used it the more I like it.

Here is a simple example of a refactored test using the fluent interface.


public void Should_leverage_the_repository_to_retrieve_all_games()
    using (mockery.Record())
        Expect.Call(repository.Find(null)).Return(new List<Game>()).IgnoreArguments();

    using (mockery.Playback())


public void Should_leverage_the_repository_to_retrieve_all_games()
        .Expecting(() => Expect.Call(repository.Find(null)).Return(new List<Game>()).IgnoreArguments())
        .Verify(() => service.GetGames());

A whole 50% smaller!


~ by Tim on 30 October 2008.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: