Last active
May 3, 2018 07:36
-
-
Save glennstephens/13e174be66e7294ec1c7eb1ac409d0b5 to your computer and use it in GitHub Desktop.
If you have an app that is designed to be used over a long period of time (say a year). To write tests you need to simulate the passage of time. I've been using this little helper to allow to simulated tests in the fourth dimension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instead of using calls like DateTime.Now, use TheDateTime.Now instead. In your production code it will default to using | |
// the read details and for testing you would make calls like: | |
// TheDateTime.SwitchToMockDate(new DateTime(2018, 12, 3); | |
// and then test what the outcome would be. It assumes that you have this throughout | |
public static class TheDateTime | |
{ | |
static Lazy<LiveDateTimeFunctions> _liveDates = new Lazy<LiveDateTimeFunctions>(() => new LiveDateTimeFunctions()); | |
static Lazy<MockDateTimeFunctions> _mockDates = new Lazy<MockDateTimeFunctions>(() => new MockDateTimeFunctions()); | |
static IDateTimeFunctions _instance = _liveDates.Value; | |
public static void SwitchToMockDate(DateTime currentTime) | |
{ | |
_instance = _mockDates.Value; | |
_instance.SetNow(currentTime); | |
} | |
public static void SetNow(DateTime value) | |
{ | |
SwitchToMockDate(value); | |
} | |
public static void SwitchToRealTime() | |
{ | |
_instance = _liveDates.Value; | |
} | |
public static void AddDays(int days) | |
{ | |
_instance.AddDays(days); | |
} | |
public static DateTime Now | |
{ | |
get | |
{ | |
return _instance.GetNow(); | |
} | |
} | |
public static DateTime MinValue | |
{ | |
get | |
{ | |
return _instance.GetMinimumValue(); | |
} | |
} | |
} | |
public interface IDateTimeFunctions | |
{ | |
DateTime GetNow(); | |
void SetNow(DateTime time); | |
void AddDays(int days); | |
DateTime GetMinimumValue(); | |
} | |
public class LiveDateTimeFunctions : IDateTimeFunctions | |
{ | |
public DateTime GetNow() | |
{ | |
return DateTime.Now; | |
} | |
public void SetNow(DateTime time) | |
{ | |
// Don't need this for the live version | |
} | |
public void AddDays(int days) | |
{ | |
// Don't need this for the live version | |
} | |
public DateTime GetMinimumValue() | |
{ | |
return DateTime.MinValue; | |
} | |
} | |
/// <summary> | |
/// This class is used to allow for mocking mechanisms to simulate what would happen with someones training | |
/// and the right situations occur | |
/// </summary> | |
public class MockDateTimeFunctions : IDateTimeFunctions | |
{ | |
DateTime CurrentTime = DateTime.Now; | |
public DateTime GetNow() | |
{ | |
return CurrentTime; | |
} | |
public void SetNow(DateTime time) | |
{ | |
CurrentTime = time; | |
} | |
public void AddDays(int days) | |
{ | |
CurrentTime = CurrentTime.AddDays(days); | |
} | |
public DateTime GetMinimumValue() | |
{ | |
return DateTime.MinValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment