Following on from this question...I'm trying to unit test the following scenario:
I have a class that allows one to call a method to perform some action and if it fails wait a second and recall that method.
Say I wanted to call a method DoSomething()...but in the event of an exception being thrown by DoSomething() I want to be able to retry calling it up to a maximum of 3 times but wait 1 second between each attempt. The aim of the unit test, in this case, is to verify that when we called DoSomething() 3 times with 1 second waits between each retry that the total time taken is >= 3 seconds.
Unfortunately, the only way I can think to test it, is to time it using a Stopwatch....which has two side effects...
What would be nice is if there were a way to mock out this dependency on time so that my test is able to run quicker and be fairly consistent in it's results. Unfortunately, I can't think of a way to do so...and so I thought I'd ask and see if any of you out there have ever encountered this problem...
You could make a Waiter
class who provides a method, Wait(int)
that waits the amount of time specified. Make tests for this method, then in your unit test, pass in a mocked up version that simply keeps track of how long it was asked to wait, but returns immediately.
For instance (C#):
interface IWaiter
{
void Wait(int milliseconds);
}
class Waiter : IWaiter
{
public void Wait(int milliseconds)
{
// not accurate, but for the sake of simplicity:
Thread.Sleep(milliseconds);
}
}
class MockedWaiter : IWaiter
{
public void Wait(int milliseconds)
{
WaitedTime += milliseconds;
}
public int WaitedTime { get; private set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With