MOQ is a very useful mocking library available to mock your code, and doesn’t require any prior knowledge or experience with mocking concepts.
You can download DLL from http://code.google.com/p/moq/
Below is the example I created to demo simple MOQ:
First, I created a couple of business classes
public class Emailer
{
private IEmailService EmailService { get; set; }
public Emailer(IEmailService emailService)
{
EmailService = emailService;
}
public void SendBatchEmails()
{
Dictionary<string, string> emails = new Dictionary<string, string>
{
{"foo1@hotmail.com","foo1"},
{"foo2@hotmail.com","foo2"},
{ "foo3@hotmail.com","foo3"}
};
foreach (KeyValuePair<string, string> email in emails)
{
if (!EmailService.SendEmail(email.Key, email.Value))
{
throw new ApplicationException("Some message here");
}
}
}
}
public class EmailService: IEmailService
{
public bool SendEmail(string emailAddress, string message)
{
return false;
}
}
public interface IEmailService
{
bool SendEmail(string emailAddress, string message);
}
The code is quite straightforward. Now right click on the code and select “Create a Unit Test” from the context menu. A new test class is created. Create the first test method
[TestMethod()]
public void NonMock()
{
var emailService = new EmailService();
var emailer = new Emailer(emailService);
emailer.SendBatchEmails();
}
It seems do the job but actually there are a couple of issues. The first issue is this is not really Unit Test but integration test, the second one is the test method will fail as SendEmail will return false so an exception is thrown. Now we need MOQ to help us mocking the EmailService
public void TestWithMock()
{
var mockEmailService = new Mock<IEmailService>();
mockEmailService.Expect((x=>x.SendEmail(It.IsAny<string>(),It.IsAny<string>()))).Returns(true);
var emailer = new Emailer(mockEmailService.Object);
emailer.SendBatchEmails();
mockEmailService.VerifyAll();
}
So the above code is mock the IEmailService, It.IsAny() means we don’t care about the string and the method return true. When pass the MOCK obhect remember use mockEmailService.Object. and mockEmailService.VerifyAll();
Below code is how to test if the expected exception when SendEmail return false and exception thrown from EmailService
/// <summary>
///A test for Mock which return false,
/// expected exception thrown
///</summary>
[TestMethod()]
[ExpectedException(typeof(ApplicationException), "Some message here")]
public void TestWithMock_ReturnFalse()
{
var mockEmailService = new Mock<IEmailService>();
mockEmailService.Expect((x => x.SendEmail(It.IsAny<string>(), It.IsAny<string>()))).Returns(false);
var emailer = new Emailer(mockEmailService.Object);
emailer.SendBatchEmails();
mockEmailService.VerifyAll();
}
/// <summary>
///A test for Mock which throw exception,
///
///</summary>
[TestMethod()]
[ExpectedException(typeof(ApplicationException), "Some message here")]
public void TestWithMock_ThrowException()
{
var mockEmailService = new Mock<IEmailService>();
mockEmailService.Expect((x => x.SendEmail(It.IsAny<string>(), It.IsAny<string>()))).Throws(new ApplicationException("AA"));
var emailer = new Emailer(mockEmailService.Object);
emailer.SendBatchEmails();
mockEmailService.VerifyAll();
}
You can download code from Here