I assume you have code like
if(token.ExpirationDateTime > DateTime.Now) { // do some job to expire }
I usually provide classes with this logic with an input field of type Func<DateTime>
. for instance
public class ExpirationManager { private Func<DateTime> _nowProvider; public ExpirationManager(Func<DateTime> nowProvider) { _nowProvider = nowProvider; } }
Then the expiration code looks like
if(token.ExpirationDateTime > _nowProvider()) { // do some job to expire }
Thus, you can replace the real current system time with any DataTime that you want when you run tests (for example, unit tests).
source share