What’s the standard/recommended way to test time-dependent methods?
As an example:
I have a class Foo that has a method GetTimestamp() and GetTimestamp() should return the given time in a string that always increases lexicographically (alphabetical sort order).
What’s the best way to test this without overengineering for testing purposes?
My initial thought was to create a class constructor that accepts an IDateTimeProvider whose only property was a DateTime property, and then from the unit test I could create a dummy DateTimeProvider…
This is a simplistic case but represents what I’m wondering about.
This has a lot of other applications, such as testing date-dependent logic around leap years, etc.
This is a good resource:
Why there is no IDateTimeProvider in .NET and DateTime has Now getter?
It sounds like this is an OK approach.