You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's another option:
public class SystemTime
{
protected static Func localNow = () => DateTime.Now;
public static DateTime Now
{
get => localNow();
protected set { localNow = () => value; }
}
}
public class TestingSystemTime : SystemTime
{
public static DateTime StartForTest()
{
Now = DateTime.Now;
return Now;
}
public static DateTime Reset()
{
localNow = () => DateTime.Now;
return localNow();
}
public static DateTime AddMinutes(int mins)
{
Now = Now.AddMinutes(mins);
return Now;
}
public static DateTime AddHours(int hours)
{
Now = Now.AddHours(hours);
return Now;
}
}
Add links and code example of a testing seam for DateTime.
I've called this systemTime in the past.
Here's a possible implementation
using System;
namespace Shared.Core.Implementations
{
///
/// Testable alternative to DateTime.Now or DateTimeOffset.Now
///
public static class SystemTime
{
private static DateTime? _dateTimeHolder = null;
}
The text was updated successfully, but these errors were encountered: