These code samples are from:
http://www.ayende.com/Blog/archive/2008/06/06/Scratching-an-itch-NMemcached.aspx public class SystemTime
{
public static Func<DateTime> Now = () => DateTime.Now;
}
At first I was wondering what the point of that was. Does he really hate the name DateTime? Was he planning on changing the class one day to not use DateTime.Now and wanted a layer of indirection? Turns out that was on the right track, but not exactly it.
https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/experiments/NMemcached/NMemcached.Tests/Memcache_FlushAll_Tests.csAfter seeing his unit tests I figured it out. He changes the value of the static function SystemTime.Now from being a function call that is evaluated each time to returning a burned in value of January 1, 2000. Now his code that checks for the current time to see if something is expired will see that date. He tests this in "When_getting_item_that_has_been_expired_will_return_empty_result()". You could do something messier like adding a cache item that is set to expire in 2 seconds, sleeping 2 seconds, and then checking that it's gone, modifying your classes to allow passing in a DateTime or setting it via reflection, but this is so much neater and doesn't introduce delays into your unit tests which should run as fast as possible.