I have been trying to get more in to TDD. Currently keeping it simple with lots of Debug.Asserts
in a console application.
Part of the tests I wanted to complete was ensuring that events were raised from the object, the correct number of times, since client code will depend on these events.
So, I began thinking about how to test the events were being raised, and how I could track them. So I came up with a Monitor "pattern" (if you could call it that). This is basically a class that takes an object of the type under test in its constructor.
The events are then hooked up to the monitor, with delegates created which both count and log when the event is raised.
I then go back to my test code and do something like:
bool Test()
{
MyObject mo = new MyObject();
MyMonitor mon = new MyMonitor(mo);
// Do some tests that should cause the object to raise events..
return mon.EventCount == expectedCount;
}
This works fine, and when I intentionally busted my code, the tests failed as expected, but I wonder, is this too much "free form" test code (i.e. code without supporting tests)?
Additional Thoughts
- Do you test for events?
- How do you test for events?
- Do you think the above has any holes/room for improvement?
All input gratefully received! ^_^