Introduction
There are several mocking frameworks to be used in testing environments, such as NMock, RhinoMocks, FakeItEasy, and Moq, to isolate units to be tested from the underlying dependencies. Although Moq is a relatively new mocking framework, this framework has been adapted by the developers because it's very easy to use, not following the traditional mock pattern Record/Replay, which is very opaque and unintuitive; it supports full VS Intellisense when creating the mock objects as well as it supports the new features of Microsoft.NET 2.0/3.5/4.0 such as dynamic typing, lambda expressions and LINQ expressions in C#. So, you, as a developer, will have a very low learning curve when using mocking frameworks.
I will explain how to use this amazing mocking framework in this article.
Suppose we need to build a Calculator that provides basic arithmetic operations and a currency conversion operation (in this case, converting a dollar to Chilean pesos according to the actual exchange rate).
Let's define the ICalculator interface (see Listing 1).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalculatorPkg
{
public interface ICalculator
{
int Add(int param1, int param2);
int Subtract(int param1, int param2);
int Multipy(int param1, int param2);
int Divide(int param1, int param2);
int ConvertUSDtoCLP(int unit);
}
}
Listing 1
Let's suppose that we will consume an external service that provides the actual exchange rate for the USD and CLP. Let's define the IUSD_CLP_ExchangeRateFeed interface (see Listing 2).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MoneyExchangeRatePkg
{
public interface IUSD_CLP_ExchangeRateFeed
{
int GetActualUSDValue();
}
}
Listing 2
Now let's define the Calculator class to realize the ICalculator interface. Let's use Dependency Injection programming techniques to inject an object realizing the IUSD_CLP_ExchangeRateFeed interface using the constructor of the Calculator class. Finally, let's implement each class method (see Listing 3).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoneyExchangeRatePkg;
namespace CalculatorPkg
{
public class Calculator : ICalculator
{
private IUSD_CLP_ExchangeRateFeed _feed;
public Calculator(IUSD_CLP_ExchangeRateFeed feed)
{
this._feed = feed;
}
#region ICalculator Members
public int Add(int param1, int param2)
{
throw new NotImplementedException();
}
public int Subtract(int param1, int param2)
{
throw new NotImplementedException();
}
public int Multipy(int param1, int param2)
{
throw new NotImplementedException();
}
public int Divide(int param1, int param2)
{
return param1 / param2;
}
public int ConvertUSDtoCLP(int unit)
{
return unit * this._feed.GetActualUSDValue();
}
#endregion
}
}
Khaleek AhmadPosted Feb 21, 2017, 1:42 AM
Can please tell us what is the use of MOQ here.
Aditya KumareditedPosted Aug 27, 2012, 4:44 AMEdited Aug 27, 2012, 4:46 AM
Thanks John.
Mahesh ChandPosted Nov 9, 2010, 12:12 PM
Good start John. Would you mind adding links to Moq framework and download etc to readers will know what tool to download. Also, is it easy to integrate with Visual Studio 2010?