<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Corey Coogan &#187; Unit Test</title>
	<atom:link href="http://blog.coreycoogan.com/tag/unit-test/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.coreycoogan.com</link>
	<description>.Net, C#, ASP.NET MVC, Architecture and Design</description>
	<lastBuildDate>Sat, 04 Sep 2010 15:17:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.coreycoogan.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/ba1b8857133275abcfb1794dbd2e242e?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Corey Coogan &#187; Unit Test</title>
		<link>http://blog.coreycoogan.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.coreycoogan.com/osd.xml" title="Corey Coogan" />
	<atom:link rel='hub' href='http://blog.coreycoogan.com/?pushpress=hub'/>
		<item>
		<title>Mocking DateTime.Now</title>
		<link>http://blog.coreycoogan.com/2009/06/07/mocking-datetime-now/</link>
		<comments>http://blog.coreycoogan.com/2009/06/07/mocking-datetime-now/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 04:09:37 +0000</pubDate>
		<dc:creator>coreycoogan</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://blog.coreycoogan.com/?p=37</guid>
		<description><![CDATA[The problem is simple.  You have written some code that does some date calculation, comparison, etc. using DateTime.Now.   Now you want to write a unit test against some scenario to make sure the code acts as expected.  The question is how to do this in a repeatable, automated test environment?  For a single run or [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.coreycoogan.com&amp;blog=7719567&amp;post=37&amp;subd=coreycoogan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The problem is simple.  You have written some code that does some date calculation, comparison, etc. using DateTime.Now.   Now you want to write a unit test against some scenario to make sure the code acts as expected.  The question is how to do this in a repeatable, automated test environment?  For a single run or two, you may try to simply set your system date to the desired value and run your test, but that has some obvious limitations.</p>
<p>One idea that I really like comes from <a href="http://ayende.com/">Ayende</a> in a <a href="http://ayende.com/Blog/archive/2008/07/07/Dealing-with-time-in-tests.aspx">blog post</a> that&#8217;s almost a year old.  His solution is so simple and elegant &#8211; create a static method that returns Func&lt;DateTime&gt;, which allows you to specify a default and easily override the value in a test with a new expression.</p>
<pre class="brush: csharp;">
public static class SystemTime
{
     public static Func&lt;DateTime&gt; Now = () =&gt; DateTime.Now;
}
</pre>
<p>Now rather than use DateTime.Now, use SystemTime.Now.  To override the value in a test it&#8217;s simple:</p>
<pre class="brush: csharp;">
SystemTime.Now = () =&gt; new DateTime(2000,1,1);
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coreycoogan.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coreycoogan.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coreycoogan.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coreycoogan.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/coreycoogan.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/coreycoogan.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/coreycoogan.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/coreycoogan.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coreycoogan.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coreycoogan.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coreycoogan.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coreycoogan.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coreycoogan.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coreycoogan.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.coreycoogan.com&amp;blog=7719567&amp;post=37&amp;subd=coreycoogan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.coreycoogan.com/2009/06/07/mocking-datetime-now/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e09ede8123295427210e9a3565804401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coreycoogan</media:title>
		</media:content>
	</item>
	</channel>
</rss>