Unit testing events

Source: Internet
Author: User

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 fireExactly as often as you want themAnd 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 IntValue
{
Get{Return_ Counter ;}
Private Set{_ Counter =Value;}
}

Public VoidIncrement (ObjectO,EventargsE)
{
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 =NewEventcounter (); 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 firesExactly as often as you want it. 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 specified 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 cocould 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 voidTestiflistchangedeventisfiredoncewhenmovingelement ()
{
Responsivelist<String> Collection =NewResponsivelist<String> ();
Collection. Add ("Item 1");
Collection. Add ("Item 2");
Collection. Add ("Item 3");

IntCounter = 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.

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.