Rhino Mocks - 设置属性时引发事件
每当使用 Rhino Mocks 设置某个属性时,我想在存根对象上引发一个事件。例如
public interface IFoo
{
int CurrentValue { get; set; }
event EventHandler CurrentValueChanged;
}
设置 CurrentValue
将引发 CurrentValueChanged
事件
我已经尝试过 myStub.Expect(x => x.CurrentValue).WhenCalled(y => myStub.Raise ...
不起作用,因为该属性是可设置的,并且它说我正在对已定义为使用 PropertyBehaviour 的属性设置期望。此外,我也知道这是一种滥用。我对此不太满意的 WhenCalled
实现此目标的正确方法是什么?
I want to raise an event on a stub object whenever a certain property is set using Rhino Mocks. E.g.
public interface IFoo
{
int CurrentValue { get; set; }
event EventHandler CurrentValueChanged;
}
Setting CurrentValue
will raise the CurrentValueChanged
event
I have tried myStub.Expect(x => x.CurrentValue).WhenCalled(y => myStub.Raise...
which doesn't work because the property is settable and it says I'm setting expectations on a property which is already defined to use PropertyBehaviour. Also I am aware that this is an abuse of WhenCalled
which I'm none too happy about.
What the correct way of achieving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能创建了一个存根,而不是一个模拟。唯一的区别是存根默认具有属性行为。
所以完整的实现如下:
You most probably created a stub, not a mock. The only difference is that the stub has property behavior by default.
So the full implementation is something like the following: