---
title: "Testing that an event was raised"
url: "https://osherove.com/blog/2009/5/23/testing-that-an-event-was-raised.md"
canonical: "https://osherove.com/blog/2009/5/23/testing-that-an-event-was-raised.html"
published: "2009-05-23T11:25:20"
updated: "2009-05-23T11:25:20"
tags: ["Agile", "Art Of Unit Testing", "Unit Testing"]
author: "Roy Osherove"
description: "This question keeps coming up: “How can I test that an event was actually raised from my class under test?” actually, there is an easy way to check if an event was raised. you subscribe to the event in your test, and in…"
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/"
---

# Testing that an event was raised

By Roy Osherove May 23, 2009 In [Agile](https://osherove.com/blog/category/agile), [Art Of Unit Testing](https://osherove.com/blog/category/art-of-unit-testing), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published May 23, 2009; technical details may be outdated.

This question keeps coming up: “How can I test that an event was actually raised from my class under test?”

actually, there is an easy way to check if an event was raised.
you subscribe to the event in your test, and in the event handler you set a boolean flag to true. then you just assert on the flag.
here is a quick example:

**Code:**

public void Test()
{
bool wasRaised=false;
var button = new Button;
button.Click += ()=> wasRaised=true;
button.DoSomethingThatShouldHaveTriggeredTheEvent();
Assert.IsTrue(wasRaised);
}

**If you feel less comfortable using lambdas:**

public void Test()
{
bool wasRaised=false;
var button = new Button;
button.Click += delegate

> {
>
> wasRaised=true
>
> };

button.DoSomethingThatShouldHaveTriggeredTheEvent();
Assert.IsTrue(wasRaised);
}

Unfortunately, with VB.NET’s current version, doing this in a single method is next to impossible, so you are forced to register to the event with a method at the class level, and check that:

**Code (VB.NET):**

dim wasRaised as Boolean=false;

**public sub test()
**

wasRaised=false
dim button = new Button()
AddHandler( button.Click , AddressOf(OnClick))

button.DoSomethingThatShouldHaveTriggeredTheEvent()
Assert.IsTrue(wasRaised);

**end sub**

**public sub OnClick(source as object,e as EventArgs)**

wasRaised=true

**end sub**

## More in Agile

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

- Jun 2, 2009

    ### [Upcoming SilverUnit Preview](https://osherove.com/blog/2009/6/2/upcoming-silverunit-preview.html)

- May 23, 2009

    ### [SilverUnit Project Template and new version](https://osherove.com/blog/2009/5/23/silverunit-project-template-and-new-version.html)

- May 22, 2009

    ### [DateTime.LastYear](https://osherove.com/blog/2009/5/22/datetimelastyear.html)

- May 20, 2009

    ### [Art Of Unit Testing (The Samurai Book)– Get it now, it’s done.](https://osherove.com/blog/2009/5/20/art-of-unit-testing-the-samurai-book-get-it-now-its-done.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/
