- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有接口(interface)
public interface IBar
{
}
和
public interface IFoo
{
event EventHandler<IBar> MyEvent;
}
和一个类
public class Foobar
{
public Foobar(IFoo foo)
{
foo.MyEvent += MyEventMethod;
}
private void MyEventMethod(object sender, IBar bar)
{
// do nothing
}
}
现在我想使用 Moq 4 对这段出色的代码进行单元测试:
[Test]
public void MyTest()
{
Mock<IFoo> foo = new Mock<IFoo>();
Mock<IBar> bar = new Mock<IBar>();
Foobar foobar = new Foobar(foo.Object);
foo.Raise(e => e.MyEvent += null, bar.Object);
}
根据我的理解,Foobar.MyEventMethod 应该通过 raise 来调用。发生的情况是我收到一个运行时异常,显示 System.Reflection.TargetParameterCountEception {"Parameter count mismatch."}。
有趣的是:当我在单元测试中提出以下内容时:
foo.Raise(e => e.MyEvent += null, EventArgs.Empty, bar.Object);
一切如我所愿。谁能解释为什么调用需要三个参数?
谢谢
最佳答案
我假设您当时使用的是 .NET 4.5。 Type constraint was removed from EventHandler<TEventArgs>
它允许你做这样的事情:
event EventHandler<IBar> MyEvent;
在哪里IBar
只是一些接口(interface)。
IN 4.0,具有约束限制 TEventArgs
可分配给 EventArgs
类型,您的代码将无法编译。
因此(IBar
不是从 EventArgs
派生的),Moq 不会将您的事件视为 "corresponding to Event Handler pattern" ,并将其视为任何其他委托(delegate):
// Raising a custom event which does not adhere to the EventHandler pattern
...
// Raise passing the custom arguments expected by the event delegate
mock.Raise(foo => foo.MyEvent += null, 25, true);
这意味着您必须提供所有参数,包括发件人。
关于c# - 使用 Moq 实例引发 EventHandler<TEventArgs> 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29147168/
我想问一下EventHandler之间的区别和 EventHandler . 之前我实现了一个 EventHandler ,带有自定义 EventArgs,可以从用户控件传递到父页面。 我想我需要申请
在C#中使用和使用有根本区别吗 event EventHandler和 EventHandler因为它们都产生与我所看到的相同的效果,除了使用 event 关键字会在智能感知中为您提供不同的图标。 最
我向事件添加了一个方法,但现在想在某些情况下从这些事件上删除该方法: public MyClassConstructor() { otherClassObj.OnMyDataReceived
我向事件添加了一个方法,但现在想在某些情况下从这些事件上删除该方法: public MyClassConstructor() { otherClassObj.OnMyDataReceived
最近一直在想这段代码有没有什么显着的区别: public event EventHandler SomeEvent; 还有这个: public delegate void MyEventHandler
假设你有这样的方法 private void FireEvent(EventHandler eventHandler, T eventArgs, string name) { var hand
我正在使用 GWT MVP 来开发应用程序。我看到 GWT MVP 代码中可以有两种事件处理程序,但我不太确定应该在哪个地方使用哪种事件处理程序: 1) AppController 中的 Handle
我看到一些代码使用 -= new EventHandler(anEvent) ,你能告诉我与 ' += new EventHandler' 有什么不同吗? 谢谢 最佳答案 一个将委托(delegate
我遇到了这样的 jQuery 片段: selector.eventHandler(...some code...).eventHandler(...some code...); 它有什么作用? 最佳答
我试图在指定时间内淡出一个文本框。此代码适用于 Windows Phone,但不适用于 Windows 8 应用程序。我进行了更改以修复尽可能多的错误。但我无法解决其中一个问题:cannot impl
我正在尝试在 Windows Phone 应用程序中实现一个计时器。它在 Windows Phone 应用程序 (Silverlight) 中工作正常,但它在 Windows Phone 空白应用程序
两者有什么区别? object.ProgressChanged += new EventHandler(object_ProgressChanged) object.ProgressChanged +
我已经阅读了一些关于此的主题,但我仍然不知道如何解决我的问题。我来自 Java,大部分是 C# 的新手 我想在动画结束时附加监听器: myStoryBoard.Completed += new Eve
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Can I customize automatic event handler generation in
我正在使用 axon 2.3.1 ,我有一个聚合类 public class MyAggregate extends AbstractAnnotatedAggregateRoot { @Aggre
我将 C# 与 Xamarin 结合使用,在使用 EventHandler 时出现此错误: The type or namespace name 'EventHandler' could not be
我正在阅读有关 EventHandlers 的 .NET 文档,但不理解来自 https://learn.microsoft.com/en-us/dotnet/standard/events/ 的这段
我的目的是重用从 ComboBox 类继承的 SelectedValueChanged 事件(该事件又从 ListControl 类继承) 在下面的代码中:SelectedValueChanged 标
以下代码允许在两个场景之间切换。 import javafx.application.*; import javafx.event.ActionEvent; import javafx.event.E
我有一个作业,其中我应该创建一个应用程序,将货币从卡塔尔里亚尔转换为 3 种选择之一(美元、欧元或英镑)。 我想成为一个酷男孩,并为我的应用程序提供了背景、标题和图标。但这还不够,我还决定不使用提交按
我是一名优秀的程序员,十分优秀!