Verify property is never set using Moq [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-22 10:34:50

问题


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?


回答1:


Use the following code instead:

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



来源:https://stackoverflow.com/questions/13608382/verify-property-is-never-set-using-moq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!