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:

[Test]
[Description(
    @"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.

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

About these ads
Leave a comment

15 Comments

  1. Hi. Very nice Blog. Not really what i have searched over Google, but thanks for the information.

    Reply
  2. tom

     /  April 9, 2010

    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
    it-console.exe’

    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

    Reply
    • João Angelo

       /  April 9, 2010

      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.

      Reply
      • tom

         /  April 13, 2010

        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.

  3. tom

     /  April 13, 2010

    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.

    Reply
    • João Angelo

       /  April 13, 2010

      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”)

      Reply
      • tom

         /  April 14, 2010

        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.

  4. João Angelo

     /  April 15, 2010

    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

    Reply
  5. Dharmesh Tailor

     /  November 30, 2010

    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

    Reply
    • João Angelo

       /  November 30, 2010

      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

      Reply
  6. tom

     /  November 30, 2010

    @Dharmesh: i don’t think that’s possible, but why not make a testfixture with only that test in?

    Reply
    • Dharmesh Tailor

       /  November 30, 2010

      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

      Reply
  7. tom

     /  November 30, 2010

    @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…

    Reply
    • João Angelo

       /  November 30, 2010

      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.

      Reply
  1. Unit Testing with NUnit and Moles Redux « Exceptional Code

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

Follow

Get every new post delivered to your Inbox.

Join 27 other followers