---
title: "Test event logic easily with the EventsVerifier class"
url: "https://osherove.com/blog/2005/6/13/test-event-logic-easily-with-the-eventsverifier-class.md"
canonical: "https://osherove.com/blog/2005/6/13/test-event-logic-easily-with-the-eventsverifier-class.html"
published: "2005-06-13T09:42:00"
updated: "2005-06-13T09:42:00"
tags: ["Agile Related"]
author: "Roy Osherove"
description: "While working on The Regulator I came up with a class to help me test logic pertaining to events being thrown from the tested class (event throwing logic). I tried to think what would be the easiest way to test such a…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Test event logic easily with the EventsVerifier class

By Roy Osherove June 13, 2005 In [Agile Related](https://osherove.com/blog/category/agile-related)

**Historical article** · Originally published June 13, 2005; technical details may be outdated.

While working on The Regulator I came up with a class to help me test logic pertaining to events being thrown from the tested class (event throwing logic).

I tried to think what would be the easiest way to test such a thing. Simply registering for the events in your own fixture might be good enough if you don't have to check the values of the arguments being passed in the event ('wasCalled pattern') but when the time comes to check parameter values it gets pretty ugly fast.

I wanted to come up with code that looked like this:

**\[Test\]
  public void OnSomethingHappened\_NoEventThrown()
  {
   ThrowerUnderTest test = new ThrowerUnderTest();**

   **EventsVerifier verifier = new EventsVerifier();
   verifier.ExpectNoEvent(test,"SomethingHappened");
   test.OnSomethingHappened(false);
  }**

    **\[Test\]
  public void OnSomethingHappened\_EventThrown()
  {
   ThrowerUnderTest test = new ThrowerUnderTest();**

   **EventsVerifier verifier = new EventsVerifier();
   verifier.Expect(test,"SomethingHappened");
   test.OnSomethingHappened(true);
   verifier.Verify();
  }**

Making this work was not easy. You basically need to create an event handler at runtime for the event requested, receive the event and check the parameters. I wanted to use some of my own code to do this but lucked out and had a great starting point [with this very nice piece of code from CodeProject](http://www.codeproject.com/csharp/CommonEventHandler.asp) that helps you create a generic events handler for any method. 

Once you have that, it's a pretty short way to make this work as part of a test suite. Which means, if you'd like to run code like the one above today, you can, by simply downloading this small zip file and using the class inside it. It comes with full unit tests, of course, to help you get up to speed on how it's supposed to work. There is also a small testing example there that looks a lot like the one you just saw here. It's pretty simple, and shouldn't be hard to add to your own project.

Note: Currently there is only support for testing one occurrence of the event (You can't make sure that it happens twice in a row, for example, without creating new verifier instances after each event is already raised)

[Download here](http://www.teamagile.com/downloads/TeamAgile.EventsVerifier.zip)

comments and questions are welcome.

## More in Agile Related

[All posts in Agile Related](https://osherove.com/blog/category/agile-related)

- Jun 16, 2005

    ### [Mock Objects vs. "Inherit & Override"](https://osherove.com/blog/2005/6/16/mock-objects-vs-quotinherit-amp-overridequot.html)

- Jun 15, 2005

    ### [Mock Object testing can cause trouble - here's why and possibly how to avoid it](https://osherove.com/blog/2005/6/15/mock-object-testing-can-cause-trouble-heres-why-and-possibly.html)

- Jun 5, 2005

    ### [Agile Linux? Linux Kernel gets nightly build & Test](https://osherove.com/blog/2005/6/5/agile-linux-linux-kernel-gets-nightly-build-amp-test.html)

- Jun 5, 2005

    ### [MSF Agile, Testing Legacy code: Latest slides and audio from Agile Israel meeting online](https://osherove.com/blog/2005/6/5/msf-agile-testing-legacy-code-latest-slides-and-audio-from-a.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
