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.
Original
[Test] 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()) { service.GetGames(); } }
New
[Test] public void Should_leverage_the_repository_to_retrieve_all_games() { With.Mocks(mockery) .Expecting(() => Expect.Call(repository.Find(null)).Return(new List<Game>()).IgnoreArguments()) .Verify(() => service.GetGames()); }
A whole 50% smaller!

Stumbleupon
Leave a Reply