aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/TestClock.cs
blob: 91523f2b366cac378031abacdb486f750adff70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using System;
using Timeline.Services;

namespace Timeline.Tests.Helpers
{
    public class TestClock : IClock
    {
        public DateTime? MockCurrentTime { get; set; } = null;

        public DateTime GetCurrentTime()
        {
            return MockCurrentTime.GetValueOrDefault(DateTime.Now);
        }
    }

    public static class TestClockWebApplicationFactoryExtensions
    {
        public static TestClock GetTestClock<T>(this WebApplicationFactory<T> factory) where T : class
        {
            return factory.Server.Host.Services.GetRequiredService<IClock>() as TestClock;
        }
    }
}