GithubHelp home page GithubHelp logo

eventhandlerhelper's Introduction

EventHandlerHelper

This is used to un/hook event handler dynamically using Reflection in C#. Most of the time the usage of this class is used for unit testing.

Let's take a look at the example below.

namespace { internal class { ... private readonly WeakEvent _onEventHandler = new WeakEvent(); public event EventHandler OnEventHandler { add { _onEventHandler.RegisterHandler(value); } remove { _onEventHandler.UnregisterHandler(value); } } ... } }

To test that event handler, regardless what the class is that has the event handler, to make sure that the add and remove event handler working properly, you can use this class to help you out.

This is the way it works:

  1. for every event handler that is hooked and called, the variable called 'Times' will get added.
  2. If the event handler got unhooked, then the 'Times' will not get added.

My unit test framework is using MsTest with Typemock. So, you can use this class to help you to do something like this below.

[TestMethod] public void OnEventHandlerAddRemoveCoverage() { var eventHandlerHelper = new EventHandlerHelper<> {Times = 0}; // Times is set as 0 // dynamically hook up the event handler eventHandlerHelper.HookUpDelegate(, "OnEventHandler"); // Typemock trigger the event handler Isolate.Invoke.Event(() => .OnEventHandler += null, string.Empty, new EventArgs()); // Times is set to 1 now. Assert.AreEqual(1, eventHandlerHelper.Times); // dynamicaly unhook the event handler eventHandlerHelper.UnHookDelegate(, "OnEventHandler"); // Typemock try to make a call to the event handler... but nothing happens here. Isolate.Invoke.Event(() => .OnEventHandler += null, string.Empty, new EventArgs()); // Times is still set to 1 Assert.AreEqual(1, eventHandlerHelper.Times); }

Alright... Please let me know if you have questions.... Happy Coding and Enjoy!

eventhandlerhelper's People

Contributors

joegun avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.