如何测试事件包含事件处理程序? [英] How can I test that an event contains an event handler?

查看:63
本文介绍了如何测试事件包含事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试类ARegisterEventHandlers()方法将其方法之一注册为EventHandler,以用于类B上的事件.我怎样才能做到这一点?如果要这么做,我正在使用起订量.

I want to test that class A's RegisterEventHandlers() method registers one of its methods as an EventHandler for an event on class B. How can I do that? I'm using moq, if that matters.

  • 我认为没有办法从类外部检查事件处理程序委托(如果我错了,请纠正我).
  • 如果可以触发该事件然后断言调用了我的回调,那将是很好的选择,但是如果我模拟A类的接口(并为回调设置了期望),那么我将失去实现首先是我正在测试的方法RegisterEventHandlers().
  • 模拟B类的事件将是最好的选择,但是我看不出我必须要拦截哪种方法才能做到这一点.有什么方法可以为事件设置模拟并拦截+=方法调用?
  • I don't think there's a way to inspect the event handler delegate from outside the class (please correct me if I'm wrong).
  • It'd be nice if I could trigger the event and then assert that my callback was called, but if I mock the interface of the A class (and set up an expectation for the callback) then I lose the implementation of RegisterEventHandlers(), which is the method I'm testing in the first place.
  • Mocking the B class's event would be the best option, but I don't see what method I'd have to intercept to do this. Is there some way to set up a mock for an event, and intercept the += method call?

有没有解决的办法?

推荐答案

您可以在声明事件的类之外获取事件的调用列表-但这涉及反射.以下是一个代码示例,该示例显示了如何确定在调用 a.RegisterEventHandlers之后,将哪些方法(在目标实例 a 上)添加到事件 b.TheEvent 中. ().将下面的代码粘贴到代码文件中,然后添加到表单或控制台项目中: Test test = new Test(); test.Run();

You can get the invocation list for an event outside the class declaring the event - but it involves reflection. Below is a code example showing how you can determine which methods (on target instance a) are added to the event b.TheEvent after a call to a.RegisterEventHandlers(). Paste the code below in a code file and add to a form or console project: Test test = new Test(); test.Run();

using System;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;

   public class A
   {
      B m_b = new B();

      public void RegisterEventHandlers()
      {
         m_b.TheEvent += new EventHandler(Handler_TheEvent);
         m_b.TheEvent += new EventHandler(AnotherHandler_TheEvent);
      }

      public A()
      { 
         m_b.TheEvent += new EventHandler(InitialHandler_TheEvent);
      }

      void InitialHandler_TheEvent(object sender, EventArgs e)
      { }

      void Handler_TheEvent(object sender, EventArgs e)
      { }

      void AnotherHandler_TheEvent(object sender, EventArgs e)
      { }
   }

   public class B
   {
      public event EventHandler TheEvent;
      //{
      //   //Note that if we declared TheEvent without the add/remove methods, the
      //   //following would still generated internally and the underlying member
      //   //(here m_theEvent) can be accessed via Reflection. The automatically
      //   //generated version has a private field with the same name as the event
      //   //(i.e. "TheEvent")

      //   add { m_theEvent += value; }
      //   remove { m_theEvent -= value; }
      //}
      //EventHandler m_theEvent; //"TheEvent" if we don't implement add/remove


      //The following shows how the event can be invoked using the underlying multicast delegate.
      //We use this knowledge when invoking via reflection (of course, normally we just write
      //if (TheEvent != null) TheEvent(this, EventArgs.Empty)
      public void ExampleInvokeTheEvent()
      {
         Delegate[] dels = TheEvent.GetInvocationList();
         foreach (Delegate del in dels)
         {
            MethodInfo method = del.Method;
            //This does the same as ThisEvent(this, EventArgs.Empty) for a single registered target
            method.Invoke(this, new object[] { EventArgs.Empty });
         }
      }
   }


   public class Test
   {
      List<Delegate> FindRegisteredDelegates(A instanceRegisteringEvents, B instanceWithEventHandler, string sEventName)
      {
         A a = instanceRegisteringEvents;
         B b = instanceWithEventHandler;

         //Lets assume that we know that we are looking for a private instance field with name sEventName ("TheEvent"), 
         //i.e the event handler does not implement add/remove.
         //(otherwise we would need more reflection to determine what we are looking for)
         BindingFlags filter = BindingFlags.Instance | BindingFlags.NonPublic;

         //Lets assume that TheEvent does not implement the add and remove methods, in which case
         //the name of the relevant field is just the same as the event itself
         string sName = sEventName; //("TheEvent")

         FieldInfo fieldTheEvent = b.GetType().GetField(sName, filter);

         //The field that we get has type EventHandler and can be invoked as in ExampleInvokeTheEvent
         EventHandler eh = (EventHandler)fieldTheEvent.GetValue(b);

         //If the event handler is null then nobody has registered with it yet (just return an empty list)
         if (eh == null) return new List<Delegate>();


         List<Delegate> dels = new List<Delegate>(eh.GetInvocationList());

         //Only return those elements in the invokation list whose target is a.
         return dels.FindAll(delegate(Delegate del) { return Object.ReferenceEquals(del.Target, a); });
      }

      public void Run()
      {
         A a = new A();

         //We would need to check the set of delegates returned before we call this

         //Lets assume we know how to find the all instances of B that A has registered with
         //For know, lets assume there is just one in the field m_b of A.
         FieldInfo fieldB = a.GetType().GetField("m_b", BindingFlags.Instance | BindingFlags.NonPublic);
         B b = (B)fieldB.GetValue(a);

         //Now we can find out how many times a.RegisterEventHandlers is registered with b
         List<Delegate> delsBefore = FindRegisteredDelegates(a, b, "TheEvent");

         a.RegisterEventHandlers();

         List<Delegate> delsAfter = FindRegisteredDelegates(a, b, "TheEvent");

         List<Delegate> delsAdded = new List<Delegate>();
         foreach (Delegate delAfter in delsAfter)
         {
            bool inBefore = false;
            foreach (Delegate delBefore in delsBefore)
            {
               if ((delBefore.Method == delAfter.Method)
                  && (Object.ReferenceEquals(delBefore.Target, delAfter.Target)))
               {
                  //NOTE: The check for Object.ReferenceEquals(delBefore.Target, delAfter.Target) above is not necessary 
                  //     here since we defined FindRegisteredDelegates to only return those for which .Taget == a)

                  inBefore = true;
                  break;
               }
            }
            if (!inBefore) delsAdded.Add(delAfter);
         }

         Debug.WriteLine("Handlers added to b.TheEvent in a.RegisterEventHandlers:");
         foreach (Delegate del in delsAdded)
         {
            Debug.WriteLine(del.Method.Name);
         }


      }
   }




这篇关于如何测试事件包含事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆