I have to test a method which does a certain amount of work after an interval.
while (running)
{
...
// Work
...
Thread.Sleep(Interval);
}
Interval is passed in as a parameter to the class so I can just pass in 0 or 1 but I was interested as to how to mock the system clock if this wasn’t the case.
In my test I’d like to be able to simply set the time forward by TimeSpan Interval and have the thread wake up.
I’ve never written tests for code which acts upon the executing thread before and I’m sure there are some pitfalls to avoid – please feel free to elaborate on what approach you use.
Thanks!
If you do not wish to test the fact that the thread actually sleeps, a more straightforward approach (and one that is possible) is to have an ISleepService. You can then mock this out, and then not sleep in your tests, but have an implementation that does cause a Thread.Sleep in your production code.
Example using Moq:
Update
On a design\maintenance note, if it is painful to change the constructor of “SomeClass”, or to add Dependency Injection points to the user of the class, then a service locator type pattern can help out here, e.g.: