Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify property is never set using Moq [duplicate]

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?

like image 407
g t Avatar asked Nov 28 '12 15:11

g t


People also ask

What does MOQ verify do?

Verifies that all verifiable expectations have been met.

What can be mocked with MOQ?

Unit testing is a powerful way to ensure that your code works as intended. It's a great way to combat the common “works on my machine” problem. Using Moq, you can mock out dependencies and make sure that you are testing the code in isolation.


1 Answers

Use the following code instead:

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

like image 88
Justin Bannister Avatar answered Oct 18 '22 18:10

Justin Bannister