NMock Out Parameters

I’ve often thought that NMock is under-documented. The way I’ve started to learn how to use this extremely useful library is through the well-formatted examples that Ian has left around. Practical examples often show you far more than a glossary of terms or a list of available methods.

This week Andy and Drew were wanting to test a method which had both a return value and an out parameter. A quick look through the NMock Quickstart, Tutorial, Advanced or Cheat Sheet pages didn’t show any way of achieving this. Thankfully the Blogosphere came through once again to prove that you can do this with NMock.

Say you have a method in your mockable class called:

public string AddGreeting(string userName)

you could expect to write a Stub declaration such as

Stub.On(mockObject)
.Method(AddGreeting)
.With("World")
.Will(Return.Value("Hello World"));

If your method has an out parameter as well, such as:

public string AddGreeting(string userName, out int characterCount)

then your Stub declaration will need to look like this:

string expectedUserName = "World";
Stub.On(mockObject)
.Method(AddGreeting)
.With(Is.EqualTo(expectedUserName), Is.Out)
.Will(new SetNamedParameterAction(["characterCount", 11), Return.Value("Hello World"));

The Is.Out is setting the expectation that one of the parameters you’re specifying is an Out parameter. The SetNamedParameterAction object is undocumented and has been found using reflection. This is a useful feature which hasn’t been documented. The NMock project appears to being built using continuous integration these days but that obviously doesn’t happen with the project documentation.
[Edited to update code sample to make use of Is.EqualTo() and explicitly declared variables for any non-out parameters in the method call. 18th Feb 2009]

NaNT .build file Intellisense

If you want to get some limited Intellisense when you’re editing or creating NaNT .build files then you can try the following:

  1. Copy the nant.xsd file from inside your NaNt schema directory.
  2. Paste it into your Visual Studio XML Schemas directory (e.g. C:Program FilesMicrosoft Visual Studio 8XmlSchemas
  3. Restart Visual Studio

When you next load up an existing .build file, Visual Studio should detect it correctly as an XML document. To get the Intellisense to work, you will need to add the XML Namespace to the <project> tag. Start typing the additional attribute ‘xmlns=’ within the opening <project> tag and Visual Studio should offer you the following auto-complete option:

xmlns=http://nant.sf.net/release/0.85/nant.xsd

So your full project tag should look something like :

<project xmlns="http://nant.sf.net/release/0.85/nant.xsd" 
name="Your Build Name" 
default="all" 
basedir=".">

For new .build files, you can select the Schema you want to base your new XML document on by browsing in the Schemas box in the Properties window. You should find xmlSchemas expanded and the nant.xsd entry able to be selected. Tick the nant.xsd box.

The Next Release

The Development Board - under loadNow that the development team at Esendex Towers is growing, so is the pace. Nicholas has just posted the next set of tasks on the board for our next release in a few weeks.

As you can probably see, there’s not much cork left in view! Saying that, I’ve just seen the inch-thick pile of cards that Nicholas has just removed from the current release. Whilst electronic methods of project tracking might offer lots of improvements, there’s something satisfying about hearing the riffle¬†of all our hard work!