Introduction to NMOCK
By hsue-shen Tham
Mocking
In many cases when unit testing, developers will come across a piece of functionality that is hard to test given the dependencies on other classes or components of the system. One technique often used to solve this problem is mock objects. The idea of mocking is to allow the class under test to be unit tested in isolation. mock objects allow dependent classes to be replaced with mock versions. These mock objects are then passed to the class under test. Hence the dependencies are replaced by the mock versions, while the unit under test still thinks it is dealing with the real objects.Traditionally mock instances were hand coded, but nowadays there are many tools and frameworks available to make mocking easier in either the Java world or the .NET world. there are plenty of materials available describing the Fine Art of Creating Mock Objects Either Manual Or Using Some of The Popular Java Mocking Tools. Rather Than Going Down The Same Path in Java, this Article AIMS to Touch on the Unfa Miliar area of mocking in .Net Using One of the More Popular Mocking Tools in .net.
Static vs. Dynamic
Mock tools can normally be categorized into two types, dynamic or static. Static mock tools generate mock implementations of the dependencies as extra classes. These are compiled together with the source ensuring type safety check. Dynamic mock tools generate the mock implementations at runtime without adding any additional classes. Dynamic mocking tools are normally more popular and preferred to static mocking tools as they are much easier to use and do not create any extra classes. However dynamic mocks are not type safe as compared to static mocks.
Using nmock
NMock is a mocking tool for C # that is based on dynamic proxies. It makes use of the proxy pattern, which allows a class to implement an interface and redirect calls to another object acting as a proxy. NMock generates mock implementations using dynamic proxies at runtime . This allows mock objects to be dynamically defined without adding any extra classes.Normally, interfaces of the dependencies will be used to create the mock implementation. NMock supports mocking both interface and classes. In addition it also supports properties mocking. Following will be a Quick Tutorial On Using NMOCK:
EXAMPLE
Lets start with a simple Hello example where we will test the Greet () method of the Hello class. This class depends on a Person object and will greet the Person according to his name. This is not a normal case where you would normally use mock Objects But it's a Simple Enough Example To Start and Undrstand The Basics of Useing NMOCK.
WE Define An Interface for a Person as Follows:
Public Interface IPerson
{
String name {get;}
}
..............................
Public Class Hello
{
IPerson Person;
Public Hello (iPerson Person)
{
THIS.PERSON = Person;
}
Public String Greet ()
{
Return "Hello" Person.name;
}
}
As we can see here, Hello class has a dependency on IPersonBefore we go any further with the Hello class, let's learn some basics about NMock. In NMock it is really easy to create a mock object based on a given interface or class. It is a three steps process where, first you instantiate a mock object by passing in the type of the interface or class that you are trying to mock. Then you would record the behaviour of the mock object and finally you get a working instance of the mock type through the properties of the Mock object. Here is an example of how we create a mock instance in the simplest form without recording any behaviour based on the IPerson interface.// Tell NMock which interfaces or classes you are mocking
Imock MockPerson = New DynamicMock (TypeOf (IPerson));
// Get a Mock Instance of the Specified Type
IPerson Person = (iPerson) MockPerson.mockInstance;
However normally a mock instance is not really useful unless we record the behaviour of what it is supposed to do or what to expect before it is used In the following example, we record and setup the values for the IPerson's Name property.:
// Tell Nmock Which Interfaces Or Classes You Are Mocking
Imock MockPerson = New DynamicMock (TypeOf (IPerson));
// set up the value of Values
Person.expectandReturn ("Name", "John Doe");
// Get a Mock Instance of the Specified Type
IPerson Person = (iPerson) MockPerson.mockInstance;
NMock has a long list of useful Expect methods, which can be used to setup the behaviours of the mock object such as when method A is called return B, or only return B when method A is called with parameter C, or throw Exception E if method A is called or even tell the mock object that it should not be expecting call to method A at all. What was done above is simply saying that we expect that the Name property will be accessed once only and when it is accessed it will return THE STRING "John Doe". Note That We Expect The Property Name To Be Called Only. We can verify this by calling the verify () method on the mock object. // Verify Expectations on the Mock Objects
Person.Verify ();
Here's A Quick List of What's Available for Setting Up Expections:
Expect (string methodname, object [] args)
ExpectandReturn (String MethodName, Object Returnval, Object [] ARGS)
Expectandthrow (String MethodName, Exception ExceptionVal, Object [] ARGS)
Expectnocall (String MethodName)
Now Gathering All the Basics, We can Easily Apply It in Our test for hello class.so IN Our test:
[TestFixTure]
Public Class Hellotest: Assertion
{
[TEST]
Public void testexpect ()
{
// Mock the Dependency
Imock Person = New DynamicMock (TypeOf (IPerson));
// setting up VALUES
Person.expectandReturn ("Name", "John Doe");
Hello Hello = New Hello (IPerson) Person.mockInstance;
Assertequals ("Hello John Doe", Hello.greet ());
// Verify That Name Property Is Only Accessed ONCE
Person.Verify ();
}
}