views:

104

answers:

1

I am working on new functionality for a large C# project which is mostly legacy code. The area that I'm working on handles XML schema messages, creates a schedule for their transmission and places them into some legacy timer code which notifies me when they should be sent.

Although I am new to them, Visual Studio test projects are proving useful in that I can test my code without trying to get the full system up and running (which can take upto 30 minutes on the test hardware!).

I have statically tested my event handling code, but would now like to do so dynamically. Is this possible? If so how do I go about getting the test project to wait for the event without timing out?

A: 

If I understand you correctly, you could use a wait handle to signal the event and have your test project wait for the event handle to be signaled.

void Foo()
{
   var eventSource = ...;
   var waitHandle = new ManualResetEvent(false);
   eventSource.SomeEvent += (sender, e) => waitHandle.Set();

   ...

   // Wait for the event to be fired.
   waitHandle.WaitOne();
}
Judah Himango
I see where you're going with this. I can see it being useful to capture the actual event triggered by the legacy timer, but I'm not sure that it would allow me to test the event handler within the code under test.I shall endeavour to check out a few things based on this today. Thank you.
ChrisBD
If you want to check that the event handler works as expected, you're probably going to have to fake it: either call the event handler via reflection, or use mock objects to artificially raise the event.Since this is legacy code, you can use TypeMock.NET to mock the event source and artificially raise the event.
Judah Himango