Unit Testing With NUnit and Moles

UPDATE: For a revisited version of integrating Moles with NUnit check:

Unit Testing With NUnit And Modes Redux.

Moles is a lightweight detour framework that lets you replace any .NET method (including static methods) with your own delegate.

The latest version of Microsoft Pex brought some very interesting improvements. Amongst them and the one that got me most curious was the support for using Moles with NUnit tests.

When writing unit tests is common to have tests with external dependencies not relevant to the behaviour under test. Using mocking frameworks you can get around these situations but the most popular ones like Rhino Mocks and Moq, as far as I know, cannot mock sealed types or static methods. Typemock however places no constraints but is not free.

With Moles you have greater flexibility and the delegate based approach is also easy to read and understand as you can see in the next example:

    @"An exception SHOULD be thrown if the default value
    is not valid for the associated option definition type."
public void Constructor_T008()
    MOptionType optionType = new MOptionType();

    optionType.NameGet = () => "OptionTypeMock";
    optionType.CanChangeDefaultValueGet = () => true;
    optionType.MinValuesCountGet = () => 1;
    optionType.MaxValuesCountGet = () => 1;
    optionType.ValidateValueObject = x => false;

    Assert.Throws(() => {
        new OptionDefinition(optionType, "Name", "NonBooleanValue");

In the previous code snippet I configure a mole for an OptionType instance to return false in the method ValidateValue allowing me to test that the OptionDefinition constructor reacts in a proper way when the provided argument is an invalid value.

Since Moles needs the Pex profiler to work, you have to run this NUnit test using Pex command line runner with NUnit console.

pex /runner:D:\TOOLS\nunit-console.exe Parser.UnitTests.dll

About these ads

15 thoughts on “Unit Testing With NUnit and Moles”

  1. Hello,

    Thank you for the info, there doesn’t seem to be alot of info about Moles and NUnit.

    When i try to run the test using the pex profiler, i got following error:

    Unrecognized command line argument ‘/runner:C:\Program Files\NUnit 2.4.8\bin\nun

    What could be the reason? Is there an error in my command?:
    pex /runner:”C:\Program Files\NUnit 2.4.8\bin\nunit-console.exe” Be.ThomasCook.Airlines.UnitTestMoles.dll

    1. It must be related to the Pex version you are using. At the time the post was written the latest version was v0.19.41110.1 which was available for commercial use and supported the runner argument.

      I just checked the Pex download page and MS now only provides the commercial use version to MSDN subscribers. If you are using the Academic version there is the possibility that the runner argument is not supported.

      1. Thank you for your feedback. I will try to obtain a msdn subscription from my manager and see what the result is with the commercial version.

  2. I got the msdn subscriber version installed, retried my command, but i still got the error:

    Unrecognized command line argument ‘/runner:C:\Program’
    Duplicate ‘Assembly’ argument ‘2.4.8\bin\nunit-console.exe’
    Duplicate ‘Assembly’ argument ‘Be.ThomasCook.Airlines.UnitTestMoles.dll’

    You know what could be causing this? Is there something else I should be aware of? I will look for an answer and post it here if I found something.

    1. Your path to the NUnit console executable contains a space, it seems that you are not enclosing it in quotes. It should be (/runner:”C:\Program Files\NUnit 2.4.8\bin\nunit-console.exe”)

      1. I did enclosed it in quotes, it both gives the same error.
        I will try to install nunit in a path without spaces.

        Isn’t there a way to run NUnit tests with Moles in Visual Studio itself? It would be so much easier.

  3. I just had some time to check the latest version of Pex and Moles and I found out that the runner functionality was migrated from the Pex executable to one provided along with Moles. The new executable is called (moles.runner.exe) and is provided with the Moles installation.

    The usage is similar: moles.runner.exe my.tests.dll /r:nunit-console.exe

  4. thanks for a wonderful article to start with, but i have a testficture which contains 5 tests and i want to run only one test then what will be the commandline for it?

    thanks in advance

    1. You can mark each test with a specific category using NUnit CategoryAttribute and then you need to specify in the command line which category to include.

      The command line would be something like this:

      moles.runner.exe /runner:nunit-console-x86.exe My.UnitTests.dll /args:/include:MyCategoryToRun

    1. thanks Tom…but if at all i create a testfixture with only one test in it…the assembly contains more than 100 such testfixture..so is tere any way we cna execute only a particular testfixture? thanks

  5. @João Angelo i got i figured out how to run moles with NUnit.Don’t know what the problem was, but I can post the command if you like. The only bad thing is NCover doesn’t supported moles yet, so i guess we’ll have to wait a bit more for that…

    1. You’re right about NCover and it’s a pity and feel free to post the command line you used since it may prove useful to someone else.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s