Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SystemTime example #13

Open
aligneddev opened this issue Jun 16, 2020 · 1 comment
Open

Add SystemTime example #13

aligneddev opened this issue Jun 16, 2020 · 1 comment
Assignees

Comments

@aligneddev
Copy link
Owner

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;

    /// <summary>
    /// Gets the current local DateTimeOffset
    /// </summary>
    public static DateTime Now => _dateTimeHolder ?? DateTime.Now;

    /// <summary>
    /// Allow overriding the time for testing
    /// </summary>
    /// <param name="fakeDateTime"></param>
    public static void SetNow(DateTime fakeDateTime)
    {
        _dateTimeHolder = fakeDateTime;
    }
    public static void ResetNow()
    {
        _dateTimeHolder = null;
    }

    private static DateTimeOffset? _offsetHolder = null;

    /// <summary>
    /// Gets the current local DateTimeOffset
    /// </summary>
    public static DateTimeOffset NowOffset => _offsetHolder ?? DateTimeOffset.Now;

    /// <summary>
    /// Allow overriding the time for testing
    /// </summary>
    /// <param name="fakeDateTimeOffset"></param>
    public static void SetNowOffset(DateTimeOffset fakeDateTimeOffset)
    {
        _offsetHolder = fakeDateTimeOffset;
    }

    public static void ResetOffsetNow()
    {
        _offsetHolder = null;
    }
}

}

@aligneddev aligneddev self-assigned this Jun 16, 2020
@aligneddev
Copy link
Owner Author

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;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant