为什么我不能用 Moq 模拟 MouseButtonEventArgs.GetPosition() ?
我正在尝试使用 Moq 模拟 MouseButtonEventArgs.GetPosition()
,但我不断收到此错误:
System.ArgumentException:不可重写成员上的设置无效: m => m.GetPosition(It.IsAny
()) 在Moq.Mock.ThrowIfCantOverride(表达式设置,MethodInfo methodInfo) 在 Moq.Mock.<>c__DisplayClass12`2. b__11() 在 Moq.PexProtector.Invoke (Func`1 函数) 在 Moq.Mock.Setup (Mock 模拟,Expression`1 表达式) at Moq.Mock`1.Setup (Expression`1表达式)
这是我设置模拟的代码:
var mockMbEventArgs = new Mock<MouseButtonEventArgs>();
mockMbEventArgs.Setup(m => m.GetPosition(It.IsAny<IInputElement>())).Returns(new Point(10.0, 10.0));
我不确定我做错了什么,有人对如何做到这一点有任何建议吗?
I'm trying to mock MouseButtonEventArgs.GetPosition()
with Moq, but I keep receiving this error:
System.ArgumentException: Invalid setup on a non-overridable member: m => m.GetPosition(It.IsAny<IInputElement>()) at Moq.Mock.ThrowIfCantOverride(Expression setup, MethodInfo methodInfo) at Moq.Mock.<>c__DisplayClass12`2.<Setup>b__11() at Moq.PexProtector.Invoke<T>(Func`1 function) at Moq.Mock.Setup<T1,TResult>(Mock mock, Expression`1 expression) at Moq.Mock`1.Setup<TResult>(Expression`1 expression)
Here is the code where I setup my mock:
var mockMbEventArgs = new Mock<MouseButtonEventArgs>();
mockMbEventArgs.Setup(m => m.GetPosition(It.IsAny<IInputElement>())).Returns(new Point(10.0, 10.0));
I'm not sure what I'm doing wrong, does anyone have any suggestions for how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此错误意味着您正在尝试伪造一个未声明为虚拟的方法。虚拟。
Moq 在运行时生成一个类型,以便能够伪造它,生成的类型继承原始类型并覆盖其虚拟方法。由于非虚拟方法不能被覆盖(这是语言的规范,它不是 Moq 的限制),因此不可能伪造这些方法。
作为解决方案,您可以包装引发发送 MouseButtonEventArgs 事件的类,并传递您自己的类,将相关方法声明为虚拟。我认为这对你来说可能是一个小小的挑战,但值得尝试。
另一种解决方案是使用隔离框架来伪造非虚拟方法。例如,Typemock Isolator 就是一个可以做到这一点的框架。 Isolator 使用不同的机制,因此它允许伪造此类方法。
免责声明 - 我在 Typemock 工作
This error means that you're trying to fake a method which is not declared as virtual.
Moq generates a type at runtime, in order to be able to fake it the generated type inherits the from original type and overrides its virtual methods. Since non-virtual methods cannot be overridden (this is the spec of the language, it's not a limitation of Moq) it is not possible to fake these methods.
As a solution you can wrap the class that raises the event that sends MouseButtonEventArgs and pass a class of your own that declares the relevant methods as virtual. I think it might be a little challenge in your case but it's worth trying.
Another solution can be to use isolation framework that enables faking non-virtual methods. Typemock Isolator for example is a framework that can do this. Isolator uses different mechanism so it allows faking this kind of methods.
Disclaimer - I work at Typemock
MouseButtonEventArgs.GetPosition() 是一个非抽象、非虚拟方法。您无法使用 MOQ、Rhino Mocks 或大多数其他模拟框架来模拟它。
这是因为 C#/.NET 的构造方式:默认情况下所有方法都是非虚拟的。与 Java 相比,默认情况下所有方法都是虚拟的。
我没有使用过它,但我知道 Type Mock 会让你这样做,因为它通过实际重写来模拟您的代码而不是像大多数其他模拟框架那样简单继承。
MouseButtonEventArgs.GetPosition() is a non-abstract, non-virtual method. You cannot mock it with MOQ, Rhino Mocks, or most other mocking frameworks.
This is because of the way C#/.NET are structured: all methods are non-virtual by default. Compare that with Java, where all methods are virtual by default.
I have not used it, but I understand that Type Mock will let you do this because it mocks by actually rewriting your code rather than simple inheritance the way most other mocking frameworks do.