About Me

Training

Nothin But .Net Developer Bootcamp

Navigation

Search

Categories

On this page

BDD, AAA Style Testing and Rhino 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: 407
This Year: 132
This Month: 3
This Week: 0
Comments: 1082

 Friday, June 13, 2008
Friday, June 13, 2008 5:47:58 PM (Mountain Standard Time, UTC-07:00) ( C# | Programming )

Having downloaded and started to use Rhino Mocks 3.5 beta pretty much as soon as Oren released. I am very happy with the AAA style (Arrange, Act, Assert) and the readability and brevity it has brought to my tests. The current project I am on has a huge set of tests. Here is an example of using Rhino Mocks, in conjunction with some BDD style naming:

[Concern(typeof (UnitOfWorkFactory))] public class When_a_new_unit_of_work_is_requested_to_be_created : behaves_like_unit_of_work_with_scope_storage_in_play { protected override void because() { sut.Create(); } [Observation] public void Should_access_scoped_storage_to_determine_if_a_unit_of_work_is_already_active() { scoped_storage.was_told_to(item => item.DoesNotContain<IUnitOfWork>()); } } [Concern(typeof (UnitOfWorkFactory))] public class When_creating_a_unit_of_work_and_the_scoped_storage_does_not_contain_an_active_unit_of_work : behaves_like_unit_of_work_with_scope_storage_in_play { private ISession session; protected override void establish_context() { base.establish_context(); session = Dependency<ISession>(); scoped_storage.setup_result(item => item.DoesNotContain<IUnitOfWork>()).Return(true); nhibernate_session_factory.setup_result(item => item.OpenSession()).Return(session); } protected override void because() { sut.Create(); } [Observation] public void Should_store_the_newly_created_unit_of_work_in_scoped_storage() { scoped_storage.was_told_to(item => item.Store(Arg<IUnitOfWork>.Matches(uow => uow != null))); } } [Concern(typeof (UnitOfWorkFactory))] public class When_a_new_unit_of_work_is_requested_and_one_already_exists_in_scoped_storage : behaves_like_unit_of_work_with_scope_storage_in_play { private ISession session; private IUnitOfWork new_unit_of_work; private IUnitOfWork active_unit_of_work; protected override void establish_context() { base.establish_context(); session = Dependency<ISession>(); active_unit_of_work = Dependency<IUnitOfWork>(); nhibernate_session_factory.setup_result(item => item.OpenSession()).Return(session); scoped_storage.setup_result(item => item.Contains<IUnitOfWork>()).Return(true); scoped_storage.setup_result(item => item.Retrieve<IUnitOfWork>()).Return(active_unit_of_work); } protected override void because() { new_unit_of_work = sut.Create(); } [Observation] public void Should_return_a_non_disposing_unit_of_work_proxy() { new_unit_of_work.should_be_an_instance_of<NonDisposableUnitOfWork>(); } }
 

I am no longer using the automocking container so you are probably wondering what the Dependency method call is all about. It is simply a method defined on a base ContextSpecification class whose definition is as follows:

 

[Context] public abstract class ContextSpecification { [SetUp] public void setup() { unit_test_container.Initialize(); establish_context(); because(); } [TearDown] public void teardown() { after_each_specification(); unit_test_container.tear_down_and_unregister_from_dependency_registry(); } protected abstract void because(); protected abstract void establish_context(); protected virtual void after_each_specification() { } protected InterfaceType Dependency<InterfaceType>() { return MockRepository.GenerateMock<InterfaceType>(); } protected InterfaceType Stub<InterfaceType>() { return MockRepository.GenerateStub<InterfaceType>(); } }

And I have some extension methods that wrap the RhinoMocks "assertions" with more language oriented assertions: Instead of AssertWasCalled you get was_told_to, and so on.

 

Develop With Passion!!