unit testing events

In this article I will show you how you can unit test your events. I will show you a simple technique that will enable you to test if your events fire exactly as often as you want them to and I will provide you with two implementations. One implementation works well with the .NET Framework 2.0 and the second one uses .NET 3.0 in order to minimize the code necessary.

 

I am assuming you are familiar with NUnit.

 

 

In .NET 2.0 it is useful if you introduce a helper class which looks like this one:

 

 

    class EventCounter    {
private int _counter;

public int Value
{
get { return _counter; }
private set { _counter = value; }
}

public void Increment(object o, EventArgs e)
{
Value++;
}
}

The class doesn’t do much. It has only one member (Value) which is increased whenever the Increment method is called.

 

Well, I must admit that this is one of the most weird classes I ever wrote but it is quite useful in testing code... take that:

 

 

    [Test]
public void TestIfListChangedEventIsFiredOnceWhenMovingElement()
  {
ResponsiveList<string> collection = new ResponsiveList<string>();
EventCounter counter = new EventCounter(); collection.Add(“item 1”);
collection.Add(“item 2”);
collection.Add(“item 3”);

collection.ListChanged += new EventHandler(counter.Increment);
collection.MoveElement(0, 2); //moves item from index 0 to 2  
Assert.AreEqual(1, counter.Value);
}

This test checks if the ListChanged event of the class ResponsiveList is fired when elements are moved within the list using the MoveElement method.

 

Using this class you can not only test if your event fires, you can also test if your event fires exactly as often as you want it to. I found this approach extremely useful and more than once it showed me that some events where fired too often or not at all.

 

It is a good idea to not only test wether an event fires or not but also test if it fires as often as desired. Firing too many events can result in bad performance and can cause bugs which can be very hard to uncover.

 

As mentioned before the above code is very old and given the power we have with anonymous methods you don’t even have to use such a weird EventCounter class anymore. Nowadays (.NET 3.0) you could write this unit test like this:

 

[Test]
public void TestIfListChangedEventIsFiredOnceWhenMovingElement()
 {
ResponsiveList<string> collection = new ResponsiveList<string>();
collection.Add(“item 1”);
collection.Add(“item 2”);
collection.Add(“item 3”);

int counter=0;
collection.ListChanged +=   (sender,e) => { counter++; }); collection.MoveElement(0, 2); //moves item from index 0 to 2

Assert.AreEqual(1, counter);
}
As Niki points out, even in .NET 2.0 you can use a similar approach and spare yourself from using a helper class:
[Test]
public void TestIfListChangedEventIsFiredOnceWhenMovingElement()
{
ResponsiveList<string> collection = new ResponsiveList<string>();
collection.Add(“item 1”);
collection.Add(“item 2”);
collection.Add(“item 3”);

int counter=0;
collection.ListChanged += (delegate)
{
counter++;
});
collection.MoveElement(0, 2); //moves item from index 0 to 2

Assert.AreEqual(1, counter);
}
If you liked this article why not kick it.

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(unit test)