Using nmock and dynamocks in test driven development
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 Area Some Notes On How NMOCK CAN Be Used To create DynamiCock 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 Area Passed in as an ilist to a commoughexecutor 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 are not 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 Executeascalled;
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 ur Mock Commands
DynamicMock CMD1 = New DynamicMock (TypeOf (iCommand));
DynamicMock CMD2 = New DynamicMock (TypeOf (iCommand));
// set out our expertation 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 To
// mockstance 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 Expections Have Been Met.
cmd1.verify ();
cmd2.verify ();
}
}
While I Still Have Reservation About The Extent To Which MockObjects Areful, I Think That NMOCK AND ITS DYNAMICMOCKS Are A Useful Technique To Quickly Produce Loosely Coupled Test Code.