I posted this answer to another question, but it applies here if you are looking for an easy way to test DateTime.Now .
I like to create a public function that returns DateTime ( Func<DateTime> ) in a class that needs the current date / time, and set it to return DateTime.Now by default. Then, during testing, I overwrite it with a testing function that returns all the DateTimes that I want to test with.
public class MyClass { public Func<DateTime> DateTimeNow = () => DateTime.Now; public void MyMethod() { var currentTime = DateTimeNow();
Matt klein
source share