Following on from my last post NUnit & Moq – Asserting a method is called I am now going to detail, in an example, how you can set-up a Mocked object to have a particular value returned when you execute a particular method on the Mocked object.
Program code
This is the main program code that you implement in a console app, if you wish to play around with this code before implementing it.
using PlayConsole.Processor;
namespace PlayConsole
{
class Program
{
static void Main(string[] args)
{
var service = new CustomerService(new RetreiveCustomerQuery());
var customerFullName = service.GetCustomerFullName(1);
}
}
}
IRetreiveCustomerQuery Interface code
This is the interface for the database query class that we will be Mocking in our test so that it returns a known Customer object when the CustomerService’s GetCustomerFullName() method executes the query class’s Run() method.
namespace PlayConsole.Processor
{
public interface IRetreiveCustomerQuery
{
Customer Run(int id);
}
}
Retreive Customer Query implementation code
This is the implementation for the IRetreiveCustomerQuery interface.
namespace PlayConsole.Processor
{
public class RetreiveCustomerQuery : IRetreiveCustomerQuery
{
public Customer Run(int id)
{
//Some code could be here to query database for this data
return new Customer { Id = id, FirstName = "Fred", LastName = "Bloggs" };
}
}
}
CustomerService code
This is the service code. Later in our test class we will be verifying that it’s GetCustomerFullName() method returns the expected FullName of the given Customer. We are testing this by seting up a Mocked instance of IRetreiveCustomerQuery and having it return a know Customer object when it’s Run() method is executed.
namespace PlayConsole.Processor
{
public class CustomerService
{
private readonly IRetreiveCustomerQuery _query;
public CustomerService(IRetreiveCustomerQuery query)
{
_query = query;
}
public string GetCustomerFullName(int id)
{
var customer = _query.Run(id);
return string.Format("{0} {1}", customer.FirstName, customer.LastName);
}
}
}
CustomerServiceTests
This test class is where you can see the Mocking in place. You will see that a known Customer object is returned when the Run() method is called on the Mocked Object Mock<IRetreiveCustomerQuery. You can also see that we can then assert that the expected full name for the customer is returned when executing the GetCustomerFullName() method on CustomerService.
using Moq;
using NUnit.Framework;
using PlayConsole.Processor;
namespace PlayConsoleUnitTests.Processor
{
[TestFixture]
public class CustomerServiceTests
{
private Mock _query;
private CustomerService _service;
private readonly Customer _customer = new Customer
{
Id = 1,
FirstName = "John",
LastName = "Jones"
};
[SetUp]
public void Setup()
{
_query = new Mock();
_query.Setup(x => x.Run(It.IsAny()))
.Returns(_customer);
_service = new CustomerService(_query.Object);
}
[Test]
public void Succesfully_retreive_customers_fullname()
{
var fullName = _service.GetCustomerFullName(1);
var expectedFullName = string.Format("{0} {1}", _customer.FirstName, _customer.LastName);
Assert.That(fullName, Is.EqualTo(expectedFullName));
}
}
}