aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/TestClock.cs
blob: ea90305fdfb9c6f3813fb8035814900df42aed33 (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;
        }
    }
}