Some of my tests require that I need to test the date time results (like time stamps etc.). In order to be able to test the date time string I created a simple DateTime
abstraction that I'm going to use in other projects later. It should replace direct calls to DateTime.(Utc)Now
. Or should I rather call it TestValue
instead of ConstValue
?
public abstract class DateTimeProvider
{
public abstract DateTime Value { get; }
public static implicit operator DateTime(DateTimeProvider dateTimeProvider)
? ConstValue {
get; set; } return dateTimeProvider.Value;
}
}
public class NowDateTimeProvider : DateTimeProvider
{
public static publicimplicit overrideoperator DateTime(DateTimeProvider ValuedateTimeProvider)
{
get { return DateTimedateTimeProvider.Now; }Value;
}
}
public class UtcNowDateTimeProviderNowDateTimeProvider : DateTimeProvider
{
public override DateTime Value
{
get { return ConstValue ?? DateTime.UtcNow;Now; }
}
}
public class ConstDateTimeProviderUtcNowDateTimeProvider : DateTimeProvider
{
private readonly DateTime _value;
public ConstDateTimeProvider(override DateTime dateTime)Value
{
_valueget ={ dateTime;
return ConstValue ?? DateTime.UtcNow; }
public override DateTime Value { get { return _value; } }
}