Inject DateTime.Now to Aid Unit Tests

If you have logic that relies on the current system date, it’s often difficult to see how to unit test it. But by injecting a function that returns DateTime.Now we can stub the current date to be anything we want it to be.
Let’s look at an example. Here we have a simple service that creates a new user instance and saves it in a database:

Link: https://dzone.com/articles/inject-datetimenow-to-aid-unit-tests?utm_medium=feed&utm_source=feedpress.me&utm_campaign=Feed%3A+dzone%2Fwebdev