7

Possible Duplicate:
Moq - How to verify that a property value is set via the setter

I would expect the following test to fail:

public interface IObjectWithProperty
{
    int Property { get; set; }
}

[TestMethod]
public void Property_ShouldNotBeCalled()
{
    var mock = new Mock<IObjectWithProperty>();

    mock.Object.Property = 10;

    mock.Verify(x => x.Property, Times.Never());
}

However, this test passes, even though Property is clearly accessed on the line before the Verify.

So it seems that Verify actually means VerifyGet.

How should I verify that a property is never set?

Community
  • 1
  • 1
g t
  • 7,287
  • 7
  • 50
  • 85

1 Answers1

22

Use the following code instead:

mock.VerifySet(x => x.Property = It.IsAny<int>(), Times.Never());

g t
  • 7,287
  • 7
  • 50
  • 85
Justin Bannister
  • 618
  • 5
  • 11
  • Thanks, this works. The syntax seems like an easy trap to fall into, as it looks like the code is being tested when in fact it isn't. Will have to go and search my code for `Verify(*, Times.Never())` now... – g t Nov 28 '12 at 16:12