moq Getting started with moq Moqs are test doubles


Example

Mocks are meant as test doubles, that allow interactions with the mocks to be validated, they are not meant to replace the system you are testing. Examples will often demonstrate features of Moq as follows:

// Create the mock
var mock = new Mock<IMockTarget>();

// Configure the mock to do something
mock.SetupGet(x => x.PropertyToMock).Returns("FixedValue");

// Demonstrate that the configuration works
Assert.AreEqual("FixedValue", mock.Object.PropertyToMock);

// Verify that the mock was invoked
mock.VerifyGet(x => x.PropertyToMock);

Whilst this example shows the steps involved in using the mock, it is important to remember that it doesn't actually test anything, other than that the mock has been setup and used correctly. An actual test that makes use of a mock will supply the mock to the system that is to be tested. To test the following method:

public class ClassToTest
{
    public string GetPrefixedValue(IMockTarget provider)
    {
        return "Prefixed:" + provider.PropertyToMock;
    }
}

It is possible to create a mock of the dependent interface:

public interface IMockTarget
{
    string PropertyToMock { get; }
}

To create a test that actually validates the behaviour of the GetPrefixedValue method:

// Create and configure the mock to return a known value for the property
var mock = new Mock<IMockTarget>();
mock.SetupGet(x => x.PropertyToMock).Returns("FixedValue");

// Create an instance of the class to test
var sut = new ClassToTest();

// Invoke the method to test, supplying the mocked instance
var actualValue = sut.GetPrefixedValue(mock.Object);

// Validate that the calculated value is correct
Assert.AreEqual("Prefixed:FixedValue", actualValue);

// Depending on what your method does, the mock can then be interrogated to
// validate that the property getter was called.  In this instance, it's
// unnecessary since we're validating it via the calculated value.
mock.VerifyGet(x => x.PropertyToMock);