Can you Test for Events?

I'm just getting started with TDD. My code is improving.
It was very useful to be able to test for the handling of exceptions using the ExpectedException attribute.
How does one code up a test to verify that the correct firing of events? Also, it would be great for me to be able to count how many events get fired in reaction to a change in the system because I can get a huge event storm when updating objects under a datagridview.
Suggestions and resources would be appreciated.
-Michael
[511 byte] By [MichaelB] at [2008-2-24]
# 1
If you are using C#, I've found the new anonymous delegates to be perfect for this, for example:



bool fired = false;

myObject.MyEvent += delegate(object sender, EventArgs e) { fired = true; };
myObject.MethodThatFiresEvent();

Assert.IsTrue(fired, "MyObject.MyEvent did not fire.");

DavidM.Kean at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 2
Davids got pretty much the best idea. If you are working with a lot of events, then some sort of queue/hashtable/other to store events and some sort of ID to allow for later comparison would work best when you have a large number of events being fired.
dhoptonMSFT at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...

Visual Studio Team System

Site Classified