Generally speaking, you want to distract the method of getting the current date and time of the interface, for example:
public interface IDateTimeProvider { DateTime Now { get; } }
The real service will be:
public class DateTimeProvider: IDateTimeProvider { public DateTime Now { get { return DateTime.Now; } } }
And the test service will be:
public class TestDateTimeProvider: IDateTimeProvider { private DateTime timeToProvide; public TestDateTimeProvider(DateTime timeToProvide) { this.timeToProvide = timeToProvide; } public DateTime Now { get { return timeToProvide; } } }
For services that require current time, ask them to take IDateTimeProvider as a dependency. For real, pass the new DateTimeProvider (); when you are a component, go to the new TestDateTimeProvider (timeToTestFor).
Erik forbes
source share