Ayende @ Wiki

Rhino Mocks has an alternative syntax to define and replay expectations. The syntax makes heavy use of the using keyword in C#:

// Prepare mock repository
MockRepository mocks = new MockRepository();
IDependency dependency = mocks.CreateMock<IDependency>();

// Record expectations using ( mocks.Record() ) { Expect .Call( dependency.SomeMethod() ) .Return( null ); }

// Replay and validate interaction object result; using ( mocks.Playback() ) { IComponent underTest = new ComponentImplementation( dependency ); result = underTest.TestMethod(); }

// Post-interaction assertions Assert.IsNull( result );


This is a simplified example; as you can see, the expectations are recorded in one scope which also takes care (courtesy of the using keyword) of calling mocks.ReplayAll(). The interaction testing itself takes place in another scope, after which (again via using) a call is made to mocks.VerifyAll().

TBA: limitations and caveats of this technique. --TG

Up: Rhino Mocks Documentation
Next: Rhino Mocks With-Syntax

ScrewTurn Wiki version 2.0 Beta. Some of the icons created by FamFamFam.