
Please excuse the basic idea of the example it's the concept I'm showing not the calculation.
ICLOCK ADMS MSSQL HOW TO
In this post we have gone through how to remove code dependencies on DateTime and/or DateTimeOffset static methods to allow for easy unit testing. Public DateTimeOffset UtcNow => new DateTimeOffset(2020, 03, 15, 10, 0, 0, TimeSpan.Zero) The second example uses a mock class which is easily created for example: public class MockClock : IClock Var sut = new HomeController(new MockClock()) The above uses the Moq mocking framework to create a mock instance of an IClock. Var sut = new HomeController(mockClock.Object)

MockClock.Setup(x => x.UtcNow).Returns(new DateTimeOffset(2020, 03, 15, 15, 0, 0, TimeSpan.Zero)) This can be done in a couple of ways but the concept is the same create a mock clock implementation and use it when constructing the SUT aka the HomeController. Now it's all working for the implementation at run time we need to make sure it is tested. In aspnetcore 2.1 (and earlier versions) this is done in the startup class in the ConfigureServices method: public void ConfigureServices(IServiceCollection services) Once this has been done it easily be tested but how does it run? We need to make sure it has been registered with our DI container.
ICLOCK ADMS MSSQL UPDATE
Once the concrete class has been defined we can now update the HomeController to take in a dependency of IClock. Public DateTimeOffset UtcNow => DateTimeOffset.UtcNow public interface IClockĪnd then create the default implementation. I've seen it called IClock and IDateTimeOffSet so it's completely down to personal preference but the concept is the same. IClock interfaceīy using an interface to define the properties you want. So how do we resolve this to make sure that we use a well known starting point for testing purposes but at run time rely on the correct value? Abstract it away. Now this in itself is clear however can potential cause issues with testing. Return (1).Hour Īnd this will return next hour based on Utc.

The Service Under Test (SUT)įor this example the sut will be a very basic MVC controller which outputs next hour: public class HomeController
ICLOCK ADMS MSSQL FULL
The same principles can be applied to full framework using a DI container. Once we've done that we can unit test for different values and make our code more robust.Īll the examples are using the preview of aspnetcore 2.1 however none of the code is 2.1 specific so will work in current 2.x aspnetcore.

So how do we solve this problem? We need to remove the direct reliance on DateTime and DateTimeOffSet static properties. Most of the time this is due to developers using DateTime.UtcNow (or variant) inside the code being tested but the tests expecting specific values. Over the years I have experienced a number of unit tests passing without issue on a Friday but Monday morning without any changes fail. Now most of the time these items won't make a difference but if you write code which is datetime dependent or financial software which has tax year calculations in then it maybe time for some pain. It's also coming up to the time of year, in the UK, that the new tax year starts. It's come to that time of the year when summer time kicks in, also known as daylight savings, when the clocks go forward.
