<?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; Mocking</title>
	<atom:link href="http://blog.coreycoogan.com/tag/mocking/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; Mocking</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>Holy Over Mocking Batman: A Natural Progression</title>
		<link>http://blog.coreycoogan.com/2010/01/28/holy-over-mocking-batman-a-natural-progression/</link>
		<comments>http://blog.coreycoogan.com/2010/01/28/holy-over-mocking-batman-a-natural-progression/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 05:40:02 +0000</pubDate>
		<dc:creator>coreycoogan</dc:creator>
				<category><![CDATA[Alt.Net]]></category>
		<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Rhino Mocks]]></category>
		<category><![CDATA[SOLID]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Uncle Bob]]></category>
		<category><![CDATA[Interaction Testing]]></category>

		<guid isPermaLink="false">http://coreycoogan.wordpress.com/?p=258</guid>
		<description><![CDATA[Uncle Bob has been on an interesting kick lately, writing thought provoking posts with some controversial undertones. A few days ago, he posted &#8220;Mocking Mocking and Testing Outcomes&#8220;, where he mocked the [over?]use of mocking frameworks. It&#8217;s an interesting article and worth a read. I can certainly see where he is coming from with this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.coreycoogan.com&amp;blog=7719567&amp;post=258&amp;subd=coreycoogan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.objectmentor.com/omTeam/martin_r.html">Uncle Bob</a> has been on an interesting kick lately, writing thought provoking posts with some controversial undertones. A few days ago, he posted &#8220;<a href="http://blog.objectmentor.com/articles/2010/01/23/mocking-mocking-and-testing-outcomes">Mocking Mocking and Testing Outcomes</a>&#8220;, where he mocked the [over?]use of mocking frameworks. It&#8217;s an interesting article and worth a read.</p>
<p>I can certainly see where he is coming from with this post. The part that really grabbed me was the last paragraph:</p>
<blockquote><p>&#8220;Also, why do you need to verify that “verify(manager).createCredential” was called? If you really get the credentials you’re expecting from the Authenticator, why would you need to check where they come from? Isn’t it one more way to couple your test to the implementation?&#8221;</p></blockquote>
<p>I think this is the single most common pitfall of anyone who has been doing TDD with mocking for a short period of time.  The initial tendency is to test each and every interaction between the objects.  It usually starts out with large tests that make too many assertions.  It’s not uncommon in these tests to see the developer assert that:</p>
<ol>
<li>The application service layer gets called</li>
<li>A transaction is started</li>
<li>The appropriate repository method[s] are called, which can be multiple repositories</li>
<li>Some business logic is executed</li>
<li>The right value is returned</li>
</ol>
<p>Such a test is going to be very brittle and break upon any serious refactoring.  As Uncle Bob points out, do we really care about each of these interactions?  Sure, in some cases it may be important to make sure a transaction is started, but in most cases all you care about is that the right value is returned.  This is the natural progression of implementing TDD, especially when mocking gets introduced.  I think it takes the pains of these brittle tests to discover that asserting all those interactions is really not necessary.</p>
<p>The next progression starts is when the developer begins to understand what one assertion, or logical assertion group, per test really means.  This phase is a little less painful because at least now there is a separate test for each interaction, which is still typically unnecessary.  Now that the tests are broken up into logical units, the tests are easier to fix when refactoring breaks the interactions.</p>
<p>Now the final progression &#8211; the pains of these fragile tests lead to the notion that all these tests really don’t matter.  They really don’t prove anything about the quality of the software.  The tests match the implementation and the system depends a set of very brittle tests that could make refactoring too painful.  That’s when the tests get simpler.  That’s when the realization that a mock isn&#8217;t needed for everything and ever interaction doesn&#8217;t need to be tested.</p>
<p>If you are reading this and it sounds like you are in phase 1 or 2, I highly recommend you read the post “<a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/10/05/three-simple-rhino-mocks-rules.aspx">Three simple Rhino Mocks rules</a>” by <a href="http://www.lostechies.com/blogs/jimmy_bogard/default.aspx">Jimmy Bogard</a>.  It really influenced the way I write tests.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coreycoogan.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coreycoogan.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coreycoogan.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coreycoogan.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/coreycoogan.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/coreycoogan.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/coreycoogan.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/coreycoogan.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coreycoogan.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coreycoogan.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coreycoogan.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coreycoogan.wordpress.com/258/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coreycoogan.wordpress.com/258/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coreycoogan.wordpress.com/258/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.coreycoogan.com&amp;blog=7719567&amp;post=258&amp;subd=coreycoogan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.coreycoogan.com/2010/01/28/holy-over-mocking-batman-a-natural-progression/feed/</wfw:commentRss>
		<slash:comments>2</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>
		<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>