用Moq实例引发EventHandler 事件(Raise an EventHandler event with a Moq instance)

编程入门 行业动态 更新时间:2024-10-19 00:28:27
用Moq实例引发EventHandler 事件(Raise an EventHandler event with a Moq instance)

我有接口

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应该通过加注来调用。 会发生什么是我得到一个运行时异常,说System.Reflection.TargetParameterCountEception {“参数计数不匹配。”}。

有趣的事情:当我在单元测试中提出以下内容时:

foo.Raise(e => e.MyEvent += null, EventArgs.Empty, bar.Object);

一切都按我想要的那样工作。 任何人都可以解释为什么这个电话需要三个参数吗?

谢谢

I have the interfaces

public interface IBar { }

and

public interface IFoo { event EventHandler<IBar> MyEvent; }

and a class

public class Foobar { public Foobar(IFoo foo) { foo.MyEvent += MyEventMethod; } private void MyEventMethod(object sender, IBar bar) { // do nothing } }

Now I want to unit test this brilliant piece of code using 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); }

From my understanding Foobar.MyEventMethod should be called through the raise. What happens is that I get a runtime exception that says System.Reflection.TargetParameterCountEception {"Parameter count mismatch."}.

Funny thing: when I Raise the following in the unit test:

foo.Raise(e => e.MyEvent += null, EventArgs.Empty, bar.Object);

Everything works as I want it. Can anybody explain why three arguments are needed for the call?

Thank you

最满意答案

我假设你使用.NET 4.5。 类型约束从EventHandler<TEventArgs>被删除,它允许你做这样的事情:

event EventHandler<IBar> MyEvent;

IBar只是一些界面

在4.0中,由于约束限制TEventArgs可以分配给EventArgs类型,所以你的代码不会被编译。

由于这一点( IBar不是从EventArgs派生的),Moq并不认为你的事件是“对应于事件处理程序模式” ,并将其视为任何其他代表:

// 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);

这意味着您必须提供所有参数,包括发件人

I assume you use .NET 4.5 then. Type constraint was removed from EventHandler<TEventArgs> which allows you to do something like this:

event EventHandler<IBar> MyEvent;

Where IBar is just some interface.

IN 4.0, with constraint restricting TEventArgs to be assignable to EventArgs type, your code wouldn't compile.

As a result of this (IBar not deriving from EventArgs), Moq doesn't consider your event as "corresponding to Event Handler pattern", and treats it as any other 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);

Which means you have to provide all parameters, including sender.

更多推荐

本文发布于:2023-08-07 00:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   事件   EventHandler   Moq   event

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!