tag:blogger.com,1999:blog-12467669.post112397157572424762..comments2023-04-29T07:23:25.825-04:00Comments on Jay Fields' Thoughts: Testing Events in C#Jayhttp://www.blogger.com/profile/14491442812573747680noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-12467669.post-83795568521635938982008-05-05T11:38:00.000-04:002008-05-05T11:38:00.000-04:00Hi there!I have found a more elegant method on tes...Hi there!<BR/><BR/>I have found a more elegant method on testing the events. The solution is based on the Monitor class.<BR/><BR/>The thing you do is just make the application wait (with some time out) until the event occurs. In case the timeout happens you raise an exception. That is simple, but you don't need to consider TRUE as FALSE and vice versa.<BR/><BR/>You do:<BR/>1. Add the following method to the testing class<BR/><I><BR/>public void WaitResourceToUnlock(int timeout)<BR/> {<BR/> lock(sync)<BR/> {<BR/> Monitor.Wait(sync, timeout);<BR/> }<BR/> }</I><BR/><BR/>and the parameter <BR/><I>object sync = new object();</I><BR/>2. In your test write <BR/><I><BR/>WaitResourceToUnlock(2000);<BR/>Assert.IsNotNull(testableobject); </I><BR/>3. Add the event handler<BR/> <I> private void SomeEvent(object sender, Args e)<BR/> {<BR/> testableobject = new testableobject();<BR/> Monitor.Pulse(sync);<BR/> } </I><BR/><BR/>This approach works great to me :)<BR/><BR/>Cheers!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-1124233844930129502005-08-16T19:10:00.000-04:002005-08-16T19:10:00.000-04:00Thanx for explaining how to test events. I've grap...Thanx for explaining how to test events. I've grappled with the concept of testing events now for a while and this post helped me get a good understanding of the how-to...Maruis Maraishttps://www.blogger.com/profile/02551789814517819267noreply@blogger.com