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.

Setting Property Get Expectations

The previous test checks that the dependency's BulbFailure property is read but this is insufficient when testing the LightController class. We really need to return both true and false values from that property and ensure that the LightController behaves correctly in both cases. To do this we can add an expectation to the property and specify a return value. To do so we will use SetupGet.

The SetupGet method is similar to Setup, which is used to set expectations for methods. First we provide a lambda expression that specifies that we wish to capture the property being read. Next we add a further method call that determines the action we wish to take when it is read. As with methods, this can include specifying a return value, throwing an exception and several other options.

Let's replace the BulbsOKChecksBulbFailureProperty test with two new ones. These use expectations to set the return value from the ITrafficLights' BulbFailure property. We can then check that the return value of the BulbsOK method is correct when a bulb has failed and when all bulbs are working. Note that the test is still checking that the property was read, this time by calling VerifyAll to verify that all of the expectations were met.

[Test]
public void BulbsOKReturnsTrueIfNoBulbsFailed()
{
    Mock<ITrafficLights> mockLights = new Mock<ITrafficLights>();
    mockLights.SetupGet(m => m.BulbFailure).Returns(false);
    var controller = new LightController(mockLights.Object);

    var bulbsOK = controller.BulbsOK();

    mockLights.VerifyAll();
    Assert.IsTrue(bulbsOK);
}

[Test]
public void BulbsOKReturnsFalseIfBulbsFailed()
{
    Mock<ITrafficLights> mockLights = new Mock<ITrafficLights>();
    mockLights.SetupGet(m => m.BulbFailure).Returns(true);
    var controller = new LightController(mockLights.Object);

    var bulbsOK = controller.BulbsOK();

    mockLights.VerifyAll();
    Assert.IsFalse(bulbsOK);
}

Setting Property Set Expectations

You can set expectations for a property's set accessor too. As setters do not return values, these expectations will often be used to throw exceptions, so this is what our final example will do. Let's assume that we want the LightController not to react to exceptions, instead allowing them to propagate to the caller. We might want to ensure that such exceptions are not simply swallowed or ignored.

The test below checks that when the Amber property throws an exception, the exception is not lost. In this case we do not care what value we are applying to the Amber property, so the value in the lambda expression is set to It.IsAny<bool>.

[Test]
public void ExceptionsWhenSettingAmberAreNotSwallowed()
{
    Exception exception = new Exception();
    Exception caught = null;
    Mock<ITrafficLights> mockLights = new Mock<ITrafficLights>();
    mockLights.SetupSet(m => m.Amber = It.IsAny<bool>()).Throws(exception);
    var controller = new LightController(mockLights.Object);

    try { controller.SetPrepareToGo(); }
    catch (Exception ex) { caught = ex; }

    Assert.AreSame(exception, caught);
}
19 October 2011