Whats the strategy for Unit Testing a class which uses a timer?
-
After spending the last few months reading about Test Driven Development I am a complete and utter believer. Unfortunately I'm still inexperienced and need advice on unit testing a class which incorporates a timer. For example I am building an ActivityMonitor class which would be used to detect an activity timeout on a class (typically a mediator for a windows form) and reset it. I'm envisioning something like this: ActivityMonitor ..private Timer _timer; ..private EventDelegate _timeout_callback; ..ActivityMonitor(EventDelegate callback, int timer_interval); ..public Reset(); How do I test something like TimeoutRaisesCallback? I could set the timer of course and wait for the callback but that would just slow down my unit tests. Do I merely set it to something very short like a few milliseconds and take the hit? Or is there a better standard way to handle this? Also, suppose I have a class that uses the Activity Monitor: ClassThatUsesActivityMonitor ..private ActivityMonitor _activity_monitor; ..ClassThatUsesActivityMonitor() {_activity_monitor = new ActivityMonitor(new EventDelegate(Reset));} ..public Reset(); ..public DoSomething(); How do I go about unit testing that ClassThatUsesActivityMonitor reacts appropriately if the ActivityMonitor calls its Reset() function while DoSomething() is executing? I look foreward to your responses, thank you very much, Togakangaroo
-
After spending the last few months reading about Test Driven Development I am a complete and utter believer. Unfortunately I'm still inexperienced and need advice on unit testing a class which incorporates a timer. For example I am building an ActivityMonitor class which would be used to detect an activity timeout on a class (typically a mediator for a windows form) and reset it. I'm envisioning something like this: ActivityMonitor ..private Timer _timer; ..private EventDelegate _timeout_callback; ..ActivityMonitor(EventDelegate callback, int timer_interval); ..public Reset(); How do I test something like TimeoutRaisesCallback? I could set the timer of course and wait for the callback but that would just slow down my unit tests. Do I merely set it to something very short like a few milliseconds and take the hit? Or is there a better standard way to handle this? Also, suppose I have a class that uses the Activity Monitor: ClassThatUsesActivityMonitor ..private ActivityMonitor _activity_monitor; ..ClassThatUsesActivityMonitor() {_activity_monitor = new ActivityMonitor(new EventDelegate(Reset));} ..public Reset(); ..public DoSomething(); How do I go about unit testing that ClassThatUsesActivityMonitor reacts appropriately if the ActivityMonitor calls its Reset() function while DoSomething() is executing? I look foreward to your responses, thank you very much, Togakangaroo
umm...bump...not sure why I'm not getting a response