Introduction
In
this article I would explaining how to use a mocking framework for test driven
development. Sample in the current article are be developed using MOQ (mocking
framework) . I am using Visual Studio 2013 IDE
for development.
Background
Lot of developments now are getting focus on test driven
development. Test driven development does increase development affords but it
also saves us from code defects. Every time you develop a code write a unit test
against it. This will save guard your code from any invalid code or bug which might
get injected while developing new module or bugs fixes. Every time there’s a
code change make sure you are executing test against it.<o:p>
Writing unit test with 100% code coverage is also equally
important. We will discuss code coverage some time later.<o:p>
Using the code
Before we start
coding, let’s add reference to MOQ using NuGet. This will
setup required reference DLL in our project.<o:p>
Say we have class which
needs to deactivate a customer if he is already in active mode. We have
implemented this logic in business layer. At the time of test we do not wish
hit our database. Our database access layer is interface driven implementation
& interface looks like this
public interface ICustomer
{
bool IsActive { get; set; }
bool DeActivate();
}
Business Layer code
will look like this. Business layer will take Data access layer as input parameter
in it constructor. By injecting data layer as parameter we are not tightly bidding
our code with data layer. This will also help us in injecting mock object for testing. <o:p>
public class BLCustomer
{
protected ICustomer _Customer = null;
public BLCustomer(ICustomer customer)
{
_Customer = customer;
}
public void DeActivateCustomer()
{
if (_Customer.IsActive)
{
_Customer.DeActivate();
}
}
}
Before we start with our implementation, please make a note
that using MOQ you can only mock Interface, Abstract Class & Public method
which are been marked as virtual. <o:p>
Add namespace MOQ to your namespace using list. Create Mock data
access instance using Moq.Mock class
Mock<ICustomer> mockCustomerDl = new Mock<ICustomer>();
Now we need to setup
our instance to return true when we verify IsActive property. This is how we can do it.
mockCustomerDl.SetupGet(prop => prop.IsActive).Returns(true);
Here we are using SetupGet
method to mock ICustomer property IsActive. This will let Mock Instance know that
it should always return true when we access get from IsActive property. We are
using Lambda expression to access list of property inside ICustomer interface. Similarly
to mock DeActivate method we will have to use Moq.Mock.Setup. Below is the code
to setup DeActivate method.
mockCustomerDl.Setup(meth => meth.DeActivate()).Returns(true);
Now only pending item is to inject mock
object inside Customer business layer. To access mock we need to use Object property of mock instance
<o:p>
BLCustomer blCustomer = new BLCustomer(mockCustomerDl.Object);<span style="font-size: 9pt;"> </span>
<o:p>
Now our code it read for testing using Mock framework.
Complete code will look like this
Mock<ICustomer> mockCustomerDl = new Mock<ICustomer>();
mockCustomerDl.SetupGet(prop => prop.IsActive).Returns(true);
mockCustomerDl.Setup(meth => meth.DeActivate()).Returns(true);
BLCustomer blCustomer = new BLCustomer(mockCustomerDl.Object);
blCustomer.DeActivateCustomer();
<o:p>
Now when we execute our code it will not make a call
to actual data access layer but this request will be routed to Mock object.
There are some more interesting Mock
feature available under MOQ. I have listed some of them for your reference.
- Mock.Verify
- Mock.SetupSet
- Moq.Times