jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What's the point of having the IDateTime interface?

mboukhlouf opened this issue · comments

What's the point of having the IDateTime interface? The implementation MachineDateTime just calls DateTime.Now for the Now property.

You need it for Unit tests.
Sample: When you create an object you want to fill CreatedAt by the DateTime.UtcNow
The current DateTime is a kind of dependency on the environment.
How to write tests to CreatedAt?
The easiest way - create an IDateTime interface and mock it in tests.
And one more benefit - dependency on the IDateTime is explicit. You can see that this class depends on environment.

You need it for Unit tests.
Sample: When you create an object you want to fill CreatedAt by the DateTime.UtcNow
The current DateTime is a kind of dependency on the environment.
How to write tests to CreatedAt?
The easiest way - create an IDateTime interface and mock it in tests.
And one more benefit - dependency on the IDateTime is explicit. You can see that this class depends on environment.

Thank you so much.