Hi,
Nice question. There are several issues here.
First: What Timer are you using?
There are
3 timers in .NET
:arrow:
System.Threading.Timer
As this is defined in mscorlib, there is no way to mock it.
As Ohad wrote there is a simple way to overcome this buy wrapping the Timer in you own class.
:arrow:
System.Timers.Timer
There are a two ways:
1. Change the intreval to 1ms using the swap parameters:
Mock mock = MockManager.Mock(typeof (Timer), Constructor.NotMocked);
mock.ExpectSetAlways("Interval").Args(new Assign((double)1));
2. Mock the event and fire it.
Mock mock = MockManager.Mock(typeof (Timer), Constructor.NotMocked);
MockedEvent handler = mock.ExpectAddEvent("Elapsed");
//
...
// fire the event
handler.Fire(null, null);
:arrow:
System.Windows.Forms.Timer
Same as System.Timers.Timer but the event is "Tick"
1. Change Interval
Mock mock = MockManager.Mock(typeof (Timer), Constructor.NotMocked);
mock.ExpectSetAlways("Interval").Args(new Assign((double)1));
2. Fire the Event
Mock mock = MockManager.Mock(typeof (Timer), Constructor.NotMocked);
MockedEvent handler = mock.ExpectAddEvent("Tick");
//
...
// fire the event
handler.Fire(null, null);