When writing automated tests it is sometimes useful to isolate the thing(s) being tested from other parts of the system. These ‘other’ parts may still need to be provided, and sometimes the real versions are too hard or cumbersome to use. In these instances “mocked” versions can be created and used.
A mock version of something is an object that can act like the real thing but can be controlled in test code.
Moq (pronounced “mok u” or “mock”) is a library available on NuGet that allows mock objects to be created in test code and it also supports .NET Core.
Moq allows the manipulation of mock objects in many ways, including setting mock methods to return specific values, setting up properties, and matching specific arguments when the thing being tested calls the mock object.
For example, the following code shows a class that requires a constructor dependency to be able to operate:
using System;
namespace Domain
{
public interface IThingDependency
{
string JoinUpper(string a, string b);
int Meaning { get; }
}
// "Real" implementation
public class ThingDependency : IThingDependency
{
public string JoinUpper(string a, string b)
{
throw new NotImplementedException();
}
public int Meaning => throw new NotImplementedException();
}
// Class we want to test in isolation of ThingDependency
public class ThingBeingTested
{
private readonly IThingDependency _thingDependency;
public string FirstName { get; set; }
public string LastName { get; set; }
public ThingBeingTested(IThingDependency thingDependency)
{
_thingDependency = thingDependency;
}
public string X()
{
var fullName = _thingDependency.JoinUpper(FirstName, LastName);
return $"{fullName} = {_thingDependency.Meaning}";
}
}
}
Without a mock object, to write a test we could use the real ThingDependency:
[Fact]
public void TestUsingRealDependency()
{
var sut = new ThingBeingTested(new ThingDependency());
// test code
}
To isolate the ThingBeingTested from the rest of the system, Moq can create a mock version of an IThingDependency:
[Fact]
public void TestUsingMockDependency()
{
// create mock version
var mockDependency = new Mock<IThingDependency>();
// set up mock version's method
mockDependency.Setup(x => x.JoinUpper(It.IsAny<string>(), It.IsAny<string>()))
.Returns("A B");
// set up mock version's property
mockDependency.Setup(x => x.Meaning)
.Returns(42);
// create thing being tested with a mock dependency
var sut = new ThingBeingTested(mockDependency.Object);
var result = sut.X();
Assert.Equal("A B = 42", result);
}
In the preceding code, the Setup() method is used to tell the mock how to behave when it is called by the ThingBeingTested.
Moq can also be used to test the correct interactions are occurring between the ThingBeingTested and the IThingDependency:
[Fact]
public void TestUsingMockDependencyUsingInteractionVerification()
{
// create mock version
var mockDependency = new Mock<IThingDependency>();
// create thing being tested with a mock dependency
var sut = new ThingBeingTested(mockDependency.Object)
{
FirstName = "Sarah",
LastName = "Smith"
};
sut.X();
// Assert that the JoinUpper method was called with Sarah Smith
mockDependency.Verify(x => x.JoinUpper("Sarah", "Smith"), Times.Once);
// Assert that the Meaning property was accessed once
mockDependency.Verify(x => x.Meaning, Times.Once);
}
In the preceding code, the Verify method is used to check that the mock JoinUpper method is being called exactly once with the values “Sarah” and “Smith”. The test code is also expecting the method to be called exactly once.
Moq can be used to test in isolation other parts of applications such as ASP.NET Core MVC controllers, where the controller requires a dependency (such as an IFooRepository):
[Fact]
public void ContollerTest()
{
var mockDependency = new Mock<IFooRepository>();
var sut = new HomeController(mockDependency.Object);
// test code
}
To learn more about using Moq to create/configure/use mock objects check out my Mocking in .NET Core Unit Tests with Moq: Getting Started Pluralsight course.
To learn how to get started testing ASP.NET Core MVC applications check out my ASP.NET Core MVC Testing Fundamentals Pluralsight course.
You can start watching with a Pluralsight free trial.
SHARE: