Unit testing a class that uses a Timer

Viewed 30347

I've got a class that has a private member that has for type System.Windows.Forms.Timer. There's also a private method that is being called every time my timer ticks.

  1. Is it worth testing the method? (since it's private)
  2. How can I test it? (I know I can have my test class inheriting the class I want to test...)
  3. Should I be mocking my timer? Because if I have to test a class that uses an internal timer, my tests may take a lot of time to complete, right?

edit:

Actually, the method has a dependency on timing, here's the code:

private void alertTick(object sender, EventArgs e) {
    if (getRemainingTime().Seconds <= 0) {
        Display.execute(Name, WarningState.Ending, null);
        AlertTimer.Stop();
    }
    else {
        var warning = _warnings.First(x => x == getRemainingTime());

        if (warning.TotalSeconds > 0)
            Display.execute(Name, WarningState.Running, warning);
    }
}

As you can see, if the timer is running, it calls Display.execute() with different parameters from when it's ending (when the remaining time equals 0). Would that be a problem of design?

2 Answers
Related