Checking events have fired in Unit Tests

When writing unit test to check events have fired you need to add an extra delegate that also gets called back. I was refactoring my PropertyChanged system and came up with the following:

List<string> EventTracking = new List<string>();
[TestInitialize]
public void Init()
{
	obj = new TestObject();
	obj.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e) {
		EventTracking.Add(e.PropertyName);
	};
}

Then in your test check if the list contains the property name in the right order.

//Assert the events have all fired
Assert.AreEqual("PropName1", EventTracking[0]);
Assert.AreEqual("PropName2", EventTracking[1]);

 

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.