# Wednesday, August 04, 2004
I was pairing with someone last night trying to test the Command pattern which lead to a useful example of using NMock to create a dynamic Mock object to help test it.  Here are some notes on how NMock can be used to create DynamicMock objects based on an interface to quickly create loosely-coupled test code.

The command pattern allows you to wrap a command inside an object so that you can pass it to another object to execute that object.  We were using a simplified version of the pattern where we had a command interface that has an Execute method and a Results property to retrieve the results:

public interface ICommand

{

      void Execute();

 

      string Result{ get; }

}

 

Objects of this type are then passed in as an IList to a CommandExecutor that loops through the commands and calls Execute on each one:

 

public class CommandExecutor

{

      public void Execute(IList commands)

      {

            // loop through the commands and

            // call the command.Execute method

      }

}

So we wanted to write a test that ensured that the Execute method was called on each command that we provided to the CommandExecutor.  Initially we thought about sending through a concrete Command (say a ConcatenationCommand) and determining that the result was what we expected.  However, this would have coupled our design to that particular Command - if we made a change to the results that returned we would have to update our tests.  In this case we aren't concerned about the actual result, just that Execute is called on each command.  The question was how can we test this behaviour without having to worry about a concrete Command or its result?

This is where Mock objects come in (see the original Mock paper or the Pragmatic Programmer's overview).  They allow you to test the behaviour of an object rather than just its outcome.  The idea is to create a 'fake' or stub object that validates the object was used in the way we expected.  In our case we could create a MockCommand that derives from ICommand.  We could provide a boolean property - ExecuteWasCalled - on the class that could be set inside the execute command.

public class MockCommand : ICommand

{

      public bool ExecuteWasCalled;

      public void Execute()

      { ExecuteWasCalled = true; }

      public string Result { get { return "result"; }}

}

While this would work fine, since our needs are pretty simple in this case we could use NMock which provides a way of creating a DynamicMock object based on an Interface.  The DynamicMock exposes a property that can set expectations about the behaviour of an object, such as a particular method being called.  The DyanmicMock object also has a MockInstance method that returns an instance of the Interface. Using this technique saves us having to create a concrete Mock command.  Here's the resulting code:

[TestFixture]

public class CommandExecutorTests

{

      [Test]

      public void ExecuteMultipleCommands()

      {

            // Setup our mock Commands

            DynamicMock cmd1 = new DynamicMock(typeof (ICommand));

            DynamicMock cmd2 = new DynamicMock(typeof (ICommand));

 

            // Set our expectation that the execute method will
            // be called on each command

            cmd1.Expect("Execute");

            cmd2.Expect("Execute");

            CommandExecutor executor = new CommandExecutor();

 

            // Call our execute method on our executor

            // passing in an array of mock commands, using the

            // MockInstance property to create the instances.

            executor.Execute( new ICommand[] {(ICommand) md1.MockInstance, 
                                             
(ICommand) cmd2.MockInstance});

 

            // Use the Verify method on the DynamicMock to

            // ensure that all of our expectations have been met.

            cmd1.Verify();

            cmd2.Verify();

      }

}

 

While I still have reservations about the extent to which MockObjects are useful, I think that NMock and its DynamicMocks are a useful technique to quickly produce loosely coupled test code.

posted on Wednesday, August 04, 2004 10:43:33 PM (GMT Daylight Time, UTC+01:00)  #   
Related posts:
Office Server 2007: Can it help me write less code?
Server-side Excel: changing the face of financial apps?
Ray Ozzie: Services Disruption and the need for 'Client Server Service Synergy'
DDD3: Deadline for speaker submissions extended until Monday
DDD III on Sat 3 June: Call for Speakers!
I can see clearly now: NUnitAddin and NCoverBrowser
Tracked by:
"hydrocodone" (hydrocodone) [Trackback]
cuckold interracial [Trackback]
voyeur 23 [Trackback]
bbw women squashing [Trackback]
amateur project voyeur amateur voyeur [Trackback]
beautiful obese women [Trackback]
homemade sex toys [Trackback]
pregant sex [Trackback]
pregnant women fucking [Trackback]
german fisting [Trackback]
dykes on bikes [Trackback]
embarrassing pee stories [Trackback]
boss 429 [Trackback]
lesbion sex [Trackback]
free voyer [Trackback]
deep fisting [Trackback]
pogo backdoors [Trackback]
fisting sisters [Trackback]
catherine bell's breasts [Trackback]
www.amateurcurves.com [Trackback]
nudist cameltoe galleries [Trackback]
duct tape bondage [Trackback]
milf ass [Trackback]
scat lovers [Trackback]
asian piss [Trackback]
chelsea clinton pregnant [Trackback]
instructions to suck a guys dick [Trackback]
ejaculating penis [Trackback]
martina hingis upskirt [Trackback]
black azz [Trackback]
8th st latinas [Trackback]
secretary nude [Trackback]
black lesbain [Trackback]
amature allure [Trackback]
squirter [Trackback]
hardcore ladyboys [Trackback]
hosiery heaven [Trackback]
trish stratus butt naked [Trackback]
secretary slut sex [Trackback]
easy homemade sex toys [Trackback]
screaming blond fucked [Trackback]
young latina models [Trackback]
pee desperation [Trackback]
teen amateurs [Trackback]
Blacksonblondes Galleries [Trackback]
blowjob cumshot [Trackback]
pregnant tummy shots [Trackback]
lingerie modeling [Trackback]
free fisting videos [Trackback]
petite teen pics [Trackback]
free amateur sex videos [Trackback]
sex stockings sandals secretary [Trackback]
girls plump bellies [Trackback]
thick booty black girls [Trackback]
brother rapes sister [Trackback]
huge booty [Trackback]
pre ejaculation [Trackback]
deep fist [Trackback]
brunette suck [Trackback]
thick white girls [Trackback]
dogs humping girls [Trackback]
incest quest [Trackback]
hairy blonde pussy [Trackback]
amatuer fucking [Trackback]
teen voyeur [Trackback]
shaved goat [Trackback]
horse pussy [Trackback]
voyer pics [Trackback]
stocking tease [Trackback]
ladyboy escorts [Trackback]
big phat booties [Trackback]
nintendo hentai [Trackback]
convert meters to feet [Trackback]
sublime mp3 [Trackback]
asianxxx [Trackback]
scat eaters [Trackback]
alyssa milano lesbian movie [Trackback]
cubs suck [Trackback]
deep throat gag [Trackback]
cameltoe [Trackback]
nylon gallery [Trackback]
japanese ropes [Trackback]
pokemon manga [Trackback]
free femdom [Trackback]
amature allure [Trackback]
daterape [Trackback]
anal beads [Trackback]
tiffany teen password [Trackback]
naked amateurs [Trackback]
women peeing [Trackback]
voyeur fuck [Trackback]
milfseeker [Trackback]
hairy cunt [Trackback]
incesto [Trackback]
candid upskirts [Trackback]
teenage sex orgies [Trackback]
hot teenage school girls in thong [Trackback]
voyeur masterbation [Trackback]
japanese pthc [Trackback]
kinky sex toys [Trackback]
brunette getting fucked [Trackback]
seymour butts [Trackback]
teen thongs [Trackback]
young sluts [Trackback]
featherlite horse trailers [Trackback]
bbw ass [Trackback]
cum snowballs [Trackback]
anal fisting [Trackback]
forced to suck [Trackback]
girl ejaculation [Trackback]
gwinnett county public schools [Trackback]
anal stimulation [Trackback]
shemales [Trackback]
hot blondes [Trackback]
public streaking [Trackback]
voyeur post [Trackback]
preggo [Trackback]
group activities amusements virginia maryland [Trackback]
hairy grannies [Trackback]
lindsay lohan boobs [Trackback]
camel toe pictures [Trackback]
drunk flashing college chicks [Trackback]
bigcock [Trackback]
mother seduces son [Trackback]
sea squirts [Trackback]
discount sex toys [Trackback]
stacy bride [Trackback]
totally spies porn [Trackback]
homemade sex [Trackback]
interacial porn [Trackback]
"Home Insurance" (Home Insurance) [Trackback]
"Baccarat" (Baccarat) [Trackback]
"Internet BlackJack" (Internet BlackJack) [Trackback]
"Online Casino" (Online Casino) [Trackback]
"House Insurance" (House Insurance) [Trackback]
"Homeowners Insurence" (Homeowners Insurence) [Trackback]
"Home Owner Insurence" (Home Owner Insurence) [Trackback]
"political-map of italy" (political-map of italy ) [Trackback]
"free xxx videos" (free xxx videos) [Trackback]
"greek women porn" (greek women porn) [Trackback]
"health-nipples" (health-nipples ) [Trackback]
"good+home based business" (good+home based business ) [Trackback]
"free+home business opportunities" (free+home business opportunities ) [Trackback]
"Keno" (Keno) [Trackback]
"business-work at home" (business-work at home ) [Trackback]
"Online Roulette" (Online Roulette) [Trackback]
"Insurance Homeowners" (Insurance Homeowners) [Trackback]
"sean paul santana cry baby cry" (sean paul santana cry baby cry) [Trackback]
"platform-shoes on sale" (platform-shoes on sale) [Trackback]
"old gay man fucking" (old gay man fucking) [Trackback]
"principio" (principio) [Trackback]