BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Testing
.NET 3.5+

Mocking Property Expectations with Moq

Moq provides similar capabilities when mocking properties of classes or interfaces as when mocking methods. Although the same verification and expectation set up methods can be used in some circumstances, there are additional options for properties.

Testing Properties

When you are creating unit tests you should isolate the unit being tested from its dependencies. When you choose to use mock objects for this purpose, you will often need to mock properties. This may involve verifying that properties are read or changed and adding expectations to either the get or set accessors. In this article we will see how this is possible using the Moq isolation framework.

To demonstrate the use of mocked properties we need a class to be tested that has a dependency. The type below fulfils our requirements. This class provides members that allow the control of a traffic light system, including performing a test of the light bulbs. The ITrafficLights dependency represents an object that allows the individual red, amber and green light bulbs to be illuminated. This is an over-simplistic implementation but is sufficient to demonstrate the mocking we need.

public class LightController
{
    ITrafficLights _lights;

    public LightController(ITrafficLights lights)
    {
        _lights = lights;
    }

    public void SetGo()
    {
        _lights.Green = true;
        _lights.Red = _lights.Amber = false;
    }

    public void SetStop()
    {
        _lights.Red = true;
        _lights.Green = _lights.Amber = false;
    }

    public void SetPrepareToGo()
    {
        _lights.Red = _lights.Amber = true;
        _lights.Green = false;
    }

    public void SetPrepareToStop()
    {
        _lights.Amber = true;
        _lights.Red = _lights.Green = false;
    }

    public bool BulbsOK()
    {
        return !_lights.BulbFailure;
    }
}

The ITrafficLights interface is shown below. This has three write-only properties that are used to change the state of the bulbs and a read-only property that returns true when a bulb failure is detected.

public interface ITrafficLights
{
    bool Red { set; }
    bool Amber { set; }
    bool Green { set; }
    bool BulbFailure { get; }
}

The example code will use NUnit for the testing framework and Moq for the isolation framework. Add references to nunit.framework.dll and Moq.dll if you are recreating the code. You should also add the following using directives in the code files that will contain the tests.

using NUnit.Framework;
using Moq;

Verifying Property Sets

If you are using a dependency that is an interface or class with writeable properties, you may wish to use a mock object and test that the properties are correctly set. This is particularly useful if the properties are write-only, as it can be difficult to check that the correct value was applied. We can solve this with the Moq framework by using the mock's VerifySet method.

VerifySet is similar to the Verify method. It has a single parameter, which accepts a lambda expression that matches the set operation that you are expecting. If the property was set to the value you specify the operation will continue. If not, the VerifySet method will throw an exception, causing your test to fail.

The following example checks that the correct traffic light bulbs are illuminated and extinguished when executing SetGo. You can see lambda expressions representing setting the Red and Amber properties to false and Green to true.

[TestFixture]
public class TrafficLightTests
{
    [Test]
    public void GreenIsShownForGo()
    {
        Mock<ITrafficLights> mockLights = new Mock<ITrafficLights>();
        var controller = new LightController(mockLights.Object);

        controller.SetGo();

        mockLights.VerifySet(m => m.Red = false);
        mockLights.VerifySet(m => m.Amber = false);
        mockLights.VerifySet(m => m.Green = true);
    }
}

NB: Tests for the other light configurations would be included in a real project but are excluded here for brevity.

Verifying Property Gets

When you are using dependencies with readable properties you may wish to check that a mock's property was read. This can be achieved using the VerifyGet method and a suitable lambda expression. The syntax and usage of VerifyGet is the same as for VerifySet.

In the test below we are checking that when the LightController's BulksOK method is called that the BulbFailure property in the ITrafficLights dependency is read.

[Test]
public void BulbsOKChecksBulbFailureProperty()
{
    Mock<ITrafficLights> mockLights = new Mock<ITrafficLights>();
    var controller = new LightController(mockLights.Object);

    controller.BulbsOK();

    mockLights.VerifyGet(m => m.BulbFailure);
}
19 October 2011