开发者

Behavior testing with Moq: how to set expectations on a non-virtual property

I'm new to Moq and I wonder how I could write the following test if the "Bounds" property is not declared virtual.

[TestMethod]
public void SettingSize_Sets_Bounds_ExactlyOnce()
{
    // given
    var mock = new Mock<Visual>();
    var anonymSize = DrawingHelper.AnonymousSize();
    // when 
    mock.Object.Size = anonymSize;
    // then
    mock.VerifySet(visual => visual.Bounds = new Rectangle(DrawingHelper.Origin(),anonymSize), Times.Once());
}

To provide a little context, the Visual 开发者_Python百科class implements the IVIsual interface, where the Bounds property is declared. Therefore, I could use the interface to create the Mock object, but I don't see how I should change the above test to still test the behavior of the concrete IVisual implementer (Visual class).

Specifically, I want to ensure that when property Size is set, then the non-virtual property Bounds is also set.

Is this possible with Moq? If not what are the frameworks out there that would allow this?


Your when is not correct.

mock.Object.Size = anonymSize;

You are testing the mock: you are assigning a value to a property of a mock object while you need to test a real object. Provide more info on the actual object which you are trying to test.


UPDATE

You do not need a mock, you test the class while mocking the dependencies. You do not seem to have any dependency here.

So what I would do is:

// Given
Visual v = new Visual();

// When
v.Size = someSize;

// Then
Assert.That(v.Bounds == someExpectedBounds);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜