About Me

Training

Nothin But .Net Developer Bootcamp

Navigation

Search

Categories

On this page

Updates to improve readability of tests using mocks

Archive

Blogroll

 Agile Developer Venkat's Blog
 Ayende @ Blog
 B#
 Barry Gervin's Software Architecture Perspectives
 Boy Meets World
 Brad Abrams
 Canadian Developers
 Christopher Steen
 Claritude Software News
 Clemens Vasters: Enterprise Development and Alien Abductions
 Coding Horror
 Coding in an Igloo
 Dare Obasanjo aka Carnage4Life
 Darrell Norton's Blog [MVP]
 David Hayden [MVP C#]
 Don Box's Spoutlet
 Eric Gunnerson's C# Compendium
 EZWeb guy: Jeffrey Palermo [C# MVP]
 Fear and Loathing
 Generalities & Details: Adventures in the High-tech Underbelly
 Greg Young [MVP]
 Greg's Cool [Insert Clever Name] of the Day
 IanG on Tap
 Ingo Rammer's Weblog
 ISerializable - Roy Osherove's Blog
 James Kovacs' Weblog
 Jason Haley
 Jean-Luc David
 Jeremy D. Miller -- The Shade Tree Developer
 JetBrains .NET Tools Blog
 Jimmy Nilsson's weblog
 John Bristowe's Weblog
 John Papa [MVP C#]
 Jon Skeet's Coding Blog
 JonGalloway.ToString()
 Jump the Fence or Walk Around
 Lambda the Ultimate - Programming Languages Weblog
 Larkware News
 Lutz Roeder
 Marquee de Sells: Chris's insight outlet
 Martin Fowler's Bliki
 Mike Nichols - SonOfNun Technology
 MSDN Magazine - .NET Matters
 MSDN Magazine - All Articles
 OdeToCode Blogs
 Onion Blog
 Planet TW
 Raymond Lewallen [MVP]
 Rockford Lhotka
 RodMan's Corner
 Roger Johansson's blog
 Sahil Malik - blah.winsmarts.com
 Sam Gentile's Blog
 Scott Bellware [MVP]
 Scott Hanselman's Computer Zen
 ScottGu's Blog
 secretGeek
 Service Station, by Aaron Skonnard
 Signum sine tinnitu--by Guy Kawasaki
 Stephen Toub
 Steve Eichert's Blog
 Steven Rockarts
 The Blog Ride
 The Coding Hillbilly
 The Daily WTF
 TheServerSide.net: News
 Tim Gifford
 Vance Morrison's Weblog
 you've been HAACKED

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 519
This Year: 28
This Month: 0
This Week: 0
Comments: 1593

 Tuesday, October 28, 2008
Tuesday, October 28, 2008 10:43:00 PM (Mountain Standard Time, UTC-07:00) ( C Sharp )

In my ongoing quest to simplify the use of mock objects and to make the test code that I write remove the need for testing specific nomenclature. I made a small update to the BDDExtensions that I use when working with Rhino Mocks. Here is the class in question:

public static class RhinoMocksExtensions { public static VoidMethodCallOccurance<T> was_told_to<T>(this
T mock, Action<T> item) { return new VoidMethodCallOccurance<T>(mock, item); } public static IMethodOptions<R> when_told_to<T, R>(this T mock
, Function<T, R> func) where T : class { return mock.Stub(func); } }

The above 2 extension methods are the only ones I need to use when working with my mock objects. As you can see, they are just non-testing-nomenclature specific wrappers around existing rhino mocks methods.The was_told_to extension method is a method that I use to verify that a void method call message was sent to a mock object. You can see that the method returns a VoidMethodCallOccurance class that looks like this:

public class VoidMethodCallOccurance<T> { public Action<T> action; private T mock; public VoidMethodCallOccurance(T mock, Action<T> action) { this.mock = mock; this.action = action; mock.AssertWasCalled(action); } public void times(int number_of_times_the_method_
should_have_been_called) { mock.AssertWasCalled(action, y => y.Repeat.Times(number_of
_times_the_method_should_have_been_called)); } public void only_once() { times(1); } public void twice() { times(2); } }

This class allows you to specify the number of times a method was called on mock object. Which allows you to do this:

[Observation] public void should_tell_the_log_4_net_initialization_command_
to_run() { initialization_command.was_told_to(x => x.run())
.only_once(); }

as opposed to this:

[Observation] public void should_tell_the_log_4_net_initialization_command_
to_run() { initialization_command.AssertWasCalled(x => x.run()
,y => y.Repeat.Times(1)); }
 
It is a small change, but one that, IMHO, adds a lot to the
readability of the test.
 
Develop With Passion!!