Introduction to NMock
NMock to rescue the problem of writing Unit Test code to your .NET module when module has got too many dependencies.
Unit Testing Problem
The main problem in writing Unit test for a module is when the module has got dependencies, In this case until you get dependent module you cannot test your module. For example you have created a Business Logic Library which uses DataAccess Library, though Business Logic library uses interface to communicate with DataAccess Library, you cannot test your Business logic until you have DataAccess library because Business logic library has to be able to access all methods and properties of DataAccess library. What if you don’t have DataAccess library? How will you test your module?
NMock can be used to rescue this dependency problem by creating mock objects
What is mock object?
In simple words Mock object is a virtual object, which can be used as a real object but not actually a real object.
NMock is used to create Mock objects
Traditionally NUnit has concept of drivers and stubs A driver is a piece of software that is written with the sole purpose of accessing properties and methods on a library to test the functionality of that library. A stub is a piece of software whose only purpose is to provide the library under test with an implementation of any modules it is dependent on to communicate with to perform its work.
NMock is said to be an implementation of Stub.
Steps involved in using NMock to write Nunit test class for a .NET module which as got external dependencies
1. .NET module, which has to be unit tested 2. Create an Interface for Dependencies of your .NET module 3. Create an IMock object using DynamicMock of Interface 4. pass expected method and return values of test class 5. Call method of test class by passing MockInstance 6. you can compare the values by using AssertEquals in case you are using NUnit testing tool
Example
1. Hello is class to be tested, which is dependent on Person.
public class Hello { IPerson person;
public Hello(IPerson person) { this.person = person; }
public String Greet() { return "Hello " + person.Name; } }
2. Interface for Person
public interface IPerson { string Name { get; } }
3. Test case which uses mock object for Person
[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(); } }
4. Other available methods of mock object
Expect(string methodName, object[] args) ExpectAndReturn(string methodName, object returnVal, object[] args) ExpectAndThrow(string methodName, Exception exceptionVal, object[] args) ExpectNoCall(string methodName)
|
| Author: Custom Software 20 Nov 2004 | Member Level: Bronze Points : 0 |
hi nirmala, I Still dont understand how should i implement NUnit on my database application. NMock comes later. any help? any idea how can i test my applications data entry automatically by providing all the posible combination of data ?? Thanks
|