<?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/"
	>

<channel>
	<title>Peter Williams &#187; Quality</title>
	<atom:link href="http://barelyenough.org/blog/tag/quality/feed/" rel="self" type="application/rss+xml" />
	<link>http://barelyenough.org</link>
	<description>… and there is much to be learned</description>
	<lastBuildDate>Tue, 31 Aug 2010 15:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Mocking</title>
		<link>http://barelyenough.org/blog/2007/06/mocking/</link>
		<comments>http://barelyenough.org/blog/2007/06/mocking/#comments</comments>
		<pubDate>Thu, 14 Jun 2007 04:06:54 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Quality]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2007/06/mocking/</guid>
		<description><![CDATA[Mr Whitney recently posted an article in which he described mock objects as &#8220;bug aggregators&#8221;. I once held a similar point of view. Back then my belief was that test doubles (mock, stub, etc) should only be used when real objects would not work, either because they were too hard to setup or because they [...]]]></description>
			<content:encoded><![CDATA[<p>Mr Whitney recently <a href="http://www.blizzo.com/?p=58">posted an article in which he described mock objects as &#8220;bug aggregators&#8221;</a>.
I once <a href="http://pezra.barelyenough.org/blog/2005/10/test-anti-pattern-proving-the-code-is-written-like-the-code-is-written/">held a similar point of view</a>.
Back then my belief was that <a href="http://www.martinfowler.com/bliki/TestDouble.html">test doubles (mock, stub, etc)</a>
should only be used when real
objects would not work, either because they were too hard to setup or
because they were too slow.  Recently, however, my thinking towards mocks
has become a bit more nuanced<sup><a href="#footnote-less-sure-what-to-do-4b8179bb0a582b9c482d4dcd1c847709" class="footnote-ref">1</a></sup>.</p>

<p>A couple of months ago my team switched from <code>Test::Unit</code> to
<a href="http://rspec.rubyforge.org/">RSpec</a><sup><a href="#footnote-rspec-rules-4b8179bb0a582b9c482d4dcd1c847709" class="footnote-ref">2</a></sup> for our system validation needs.  As part of
that switch I read more deeply into the
<a href="http://behaviour-driven.org/">Behavior Driven Development(BDD) movement</a>, which is where tools such
as RSpec originate.  BDD is, in many ways, just a collection of
developer testing best practices combined with much better terminology
and some wicked tools (like RSpec).  While that may not sound like
much it is the biggest improvement in the testing ecosystem since the
emergence of systematic automated unit testing.</p>

<p>One of the things that the BDD promotes is the heavy use of
test doubles to isolate the tested class
from the rest of the system.  BDD takes this to such an extreme
that Martin Fowler has
<a href="http://www.martinfowler.com/articles/mocksArentStubs.html">refers to practitioners of BDD as &#8220;mockist&#8221;</a>.  So when
we switched to RSpec I
decided to set my previously held notions aside and to give full
fledged BDD a try<sup><a href="#footnote-push-until-it-breaks-4b8179bb0a582b9c482d4dcd1c847709" class="footnote-ref">3</a></sup>.</p>

<p>I have been mocking and stubbing my heart out for a couple of months.
During that time I have reached the conclusion that test doubles can
be very useful as design tools.  At the same time they can, as Mr
Whitney points out, hide some really nasty bugs.</p>

<h2>What are tests for, anyway?</h2>

<p>The tension I feel around test doubles comes from the fact that
automated unit tests, or executable specification, serve multiple
purposes.  These purposes are almost never aligned.  Improving a test
for one purpose will reduce is usefulness for other purposes.  As a
bare minimum tests serve these purposes:</p>

<ul>
<li>tests verify that the code does what you intended</li>
<li>tests provide a set of examples for how to use a class</li>
<li>tests allow for comfortable evolution of the system by protecting
against incompatible changes in the future</li>
<li>tests act as a proving ground for the design</li>
</ul>

<p>While it is the least obvious, experienced practitioners of TDD/BDD
often cite that last purpose as one of
<a href="http://homepage.mac.com/keithray/blog/2006/04/18/">the</a> <a href="http://codebetter.com/blogs/jeremy.miller/archive/2005/07/20/129540.aspx">most</a>
<a href="http://www.agiledata.org/essays/tdd.html#PartingThoughts">important</a>.   Tests have an amazing way of
surfacing poor design choices, and test doubles magnify this.  You
quickly notice interfaces that are excessively complex when you have
to mock and stub all the method calls involved.  Hard to mock
interfaces are a design smell.</p>

<p>Extensive use of test doubles radically enhances the quality of tests
as tool for validating the design.  At the same time degrades the
usefulness of the tests for protecting you from future incompatible
changes.  I don&#8217;t think test doubles <em>aggregate</em> bugs but they are
great at hiding them.  If you use test doubles extensively you <em>will</em>,
quite regularly, end up with two classes that do not work correctly
with one another in the real system, but who&#8217;s tests pass.  On the
other hand, those classes will be better designed than if you were
just using real objects.</p>

<p>In some languages (Java springs to mind) heavy use of test doubles
might also degrade the quality of the tests as examples.  If you
are using a mocking system that requires a lot of code to mock an
object it might tip the against mocking except in extreme cases.
However, in Ruby this is not really an issue.  The mock/stub library
that comes with RSpec has such a clean and pretty syntax that it takes
almost nothing away from the readability of the tests.</p>

<p>To counteract the degradation of tests as barrier to the code being
inadvertently broken in the future, mockists usually suggest adding
higher level of tests that are more about testing the system as a
whole rather than the individual parts, or <a href="http://en.wikipedia.org/wiki/Acceptance_testing">acceptance
testing</a>.  These sorts of test are usually a good
idea in their own right, but they also free you up to make more
aggressive use of test doubles to improve the design of your code.</p>

<h2>It&#8217;s all about tradeoffs</h2>

<p>As with most engineering decisions, how much to use test doubles boils
down to a set of trade-offs.  You have to decide which of the uses of
tests are most important for your project and then try to optimize
along those dimensions.  My currently feeling is that the design
benefits of using test doubles usually outweighs the costs.  Only you
and your team can decide if that is true for your project, but you
will never know until you give them a real try.</p>

<ol class="footnotes">
<li id="footnote-less-sure-what-to-do-4b8179bb0a582b9c482d4dcd1c847709">
<p>In this case, as in most, &#8220;more nuanced&#8221; is a
just a euphemism for &#8220;I am not sure what the best course of action is&#8221;.</p>
</li>
<li id="footnote-rspec-rules-4b8179bb0a582b9c482d4dcd1c847709">
<p>If you are doing ruby development you should be using
RSpec.  It is what <code>Test::Unit</code>, and all the other xUnit frameworks,
should have been.</p>
</li>
<li id="footnote-push-until-it-breaks-4b8179bb0a582b9c482d4dcd1c847709">
<p>For me, the best way to find the limits of
something new is just to use it as aggressively as possible until I
start finding situation where it does not work.  Once it starts
breaking down in real life you usually end up with a really good
feel for the limits.</p>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2007/06/mocking/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
