CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeffrey Palermo (.com)

Blog moved to www.jeffreypalermo.com

How to use NMock to assist with unit testing - level 300

When you unit test, it is important to mock dependencies.  You can use static mocks (which are really just dummy classes that implement your interface) or dynamic mocks which can save code some of the time.  NMock is one of the frameworks for dynamic mocks.  They work by creating a class instance at runtime to match your specifications.  You take the instance of this class and use it like any other object.  Below are several scenarios for using NMock to create runtime instances of an interface.  This also works for abstract classes and concrete classes with virtual members.

    1 using System;

    2 using System.Security.Principal;

    3 using NUnit.Framework;

    4 using NMock;

    5 

    6 [TestFixture]

    7 public class NMockDemo

    8 {

    9     [Test, ExpectedException(typeof(VerifyException))]

   10     public void FailsBecauseWrongThingWasPassedIn()

   11     {

   12         IMock mock = new DynamicMock(typeof(IPrincipal));

   13         mock.ExpectAndReturn("IsInRole", true, "AdminRole");

   14 

   15         IPrincipal principal = (IPrincipal) mock.MockInstance;

   16         bool result = principal.IsInRole("TechRole");

   17 

   18         mock.Verify();

   19     }

   20 

   21     [Test, ExpectedException(typeof(VerifyException))]

   22     public void FailsBecauseMethodWasntCalled()

   23     {

   24         IMock mock = new DynamicMock(typeof(IPrincipal));

   25         mock.ExpectAndReturn("IsInRole", true, "AdminRole");

   26 

   27         IPrincipal principal = (IPrincipal) mock.MockInstance;

   28 

   29         mock.Verify();

   30     }

   31 

   32     [Test]

   33     public void Suceeds()

   34     {

   35         IMock mock = new DynamicMock(typeof(IPrincipal));

   36         mock.ExpectAndReturn("IsInRole", true, "AdminRole");

   37 

   38         IPrincipal principal = (IPrincipal) mock.MockInstance;

   39         bool result = principal.IsInRole("AdminRole");

   40 

   41         mock.Verify();

   42     }

   43 

   44     [Test]

   45     public void SetupResultThatCanBeCalledManyTimes()

   46     {

   47         IMock mock = new DynamicMock(typeof(IPrincipal));

   48         mock.SetupResult("IsInRole", true, typeof(string));

   49 

   50         IPrincipal principal = (IPrincipal) mock.MockInstance;

   51 

   52         mock.Verify();

   53     }

   54 

   55     [Test]

   56     public void HowToSetupMockPropertyWithNestedMock()

   57     {

   58         IMock mock = new DynamicMock(typeof(IPrincipal));

   59         mock.SetupResult("IsInRole", true, typeof(string));

   60 

   61         DynamicMock identityMock = new DynamicMock(typeof(IIdentity));

   62         identityMock.SetupResult("Name", "someUserName");

   63         mock.SetupResult("Identity", identityMock.MockInstance);

   64 

   65         IPrincipal principal = (IPrincipal) mock.MockInstance;

   66         Console.WriteLine(principal.Identity.Name);

   67 

   68         mock.Verify();

   69     }

   70 }



Comments

Jeffrey Palermo said:

The above are not real tests. It is just a demonstration of NMock. If real tests, you would be testing a class that depends on an interface. You want to construct your class under test with an instance of the class you are mocking. To create this instance, use NMock.

It's difficult using code examples because they can never be in complete context or the post would be a book!
# November 4, 2005 11:41 AM

Joey said:

NMock Rocks! I posted another example a while back here...

http://devauthority.com/blogs/joeydotnet/archive/2005/09/23/569.aspx
# November 4, 2005 3:26 PM

irfan said:

What if return type is void..

how can u use SETupResult..

mail me if u have solution

@   irfan.mushtaq@kpitcummins.com

# June 21, 2007 12:34 PM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

This Blog

Syndication

News

Headspring Systems

View Jeffrey Palermo's profile on LinkedIn

See my new blog at .jeffreypalermo.com