Isolated Unit Test of Business Logic With NMock in TDD


Hello All

In test driven development its always needed to test a particular project tested with make it isolate ( test with removing dependency from other projects ), In this scenario we virtually create the input set which will be produced from other projects(class,methods etc) and test whether our testable methods are functioning with that input set or not.

To do that we have to make mock of the dependent objects. In the example bellow I have shown how to test a business logic layer with isolating it from data access layer.I have created three projects Web.Data , Web.Core & BL.Test .

The Web.Data project contains the Data Access Layer which will be responsible for pulling data from the data source,but as we are isolating our business logic layer from this project so in the example I will not completely  implement our desired method( as it will not be executed).

To do that we need some tools such as NMock and NUnit . We will found the NMock.dll , nunit.core & nunit.framework in both of its bin folder where we extract or install those.

our Web.Data contains only two files one is IMockDAL.cs interface and MockDAL.cs class.

IMockDAL.cs interface :

using System.Collections.Generic;

namespace Web.Data
{
public interface IMockDAL
{
/* The method will return a string list of
* active member's name from data source .
*/
List<string> GetNameListDAL(bool active);
}
}

MockDAL.cs Class :

using System;
using System.Collections.Generic;

namespace Web.Data
{
public class MockDAL:IMockDAL
{
#region IMockDAL Members

/* This method will not be executed during the test,
* as we will do mock of this method.
*/
public List<string> GetNameListDAL(bool active)
{
throw new NotImplementedException();
}

#endregion
}
}

In our Web.Core there are also two files , one is IEmployeeService.cs interface & EmployeeService.cs the class file. in this project we have to add reference of Web.Data project as it has a dependency over that.
IEmployeeService.cs  interface :

using System.Collections.Generic;
using Web.Data;

namespace Web.Core
{
public interface IEmployeeService
{
IMockDAL MockDAL { set; get; }
List<string> GetAllEmployee();
}
}

EmployeeService.cs Class :

using System.Collections.Generic;
using Web.Data;

namespace Web.Core
{
public class EmployeeService : IEmployeeService
{
/* The MockDAL property will be used to assign
* DAL through our mock object.
*/
public IMockDAL MockDAL { set; get; }

public EmployeeService()
{
MockDAL = new MockDAL();
}

#region IEmployeeService Members

/* The "GetAllEmploye" will call the "GetNameListDAL"
* from the mock object. For the sake of simplicity
* I just call the DAL,not did any other operation.
*/

public List<string> GetAllEmployee()
{
return MockDAL.GetNameListDAL(true);
}

#endregion
}
}

In BL.Test project we have to add some reference as it have dependency over  couple of projects and DLLs . We have to add reference of Web.Data & Web.Core and we also have to add NMock.dll , nunit.core & nunit.framework .In this project we have a class file named MockTest.cs

using System.Collections.Generic;
using NMock2;
using NUnit.Framework;
using Web.Data;
using Web.Core;

namespace BL.Test
{
[TestFixture]
public class MockTest
{
/* Assign property of our object that will be executed.
*/
public Mockery _mock;
public IMockDAL _mockDAL;
public IEmployeeService _employeeService;

[SetUp]
public void init()
{
/* initializing our objects.
*/
_mock = new Mockery();
_mockDAL = _mock.NewMock<IMockDAL>();
_employeeService = new EmployeeService();
_employeeService.MockDAL = _mockDAL;

}

[Test]
public void TestEmployeeCount()
{
/* In the list bellow we are creating our virtual list of
* string, which we are expecting after calling the DAL
* this list will be returned.
*/
List<string> employeeList = new List<string>();
string emp = "Maix";
string emp2 = "Pain";
employeeList.Add(emp);
employeeList.Add(emp2);


/* _mockDAL -> is our mocked DAL object.
* GetNameListDAL -> is one of the method name of the _mockDAL object.
* With(true) -> the single boolean parameter of the GetNameListDAL method.
* employeeList -> The expecter return value from the GetNameListDAL method.
*/
Expect.Once.On(_mockDAL).Method("GetNameListDAL").With(true).Will( Return.Value(employeeList));

List<string> employeeListResult = _employeeService.GetAllEmployee();

/* Verify the mock object is executed successfully with its expectation.
*/
_mock.VerifyAllExpectationsHaveBeenMet();

Assert.AreNotEqual(null, employeeListResult);
}
}
}

To make it run we can set the project as our startup project and configure the NUnit .

That’s all for the day.

BYE

User ScrumPad for your Agile based projects. 

 

, , ,

One response to “Isolated Unit Test of Business Logic With NMock in TDD”

Leave a Reply