<?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>threehv &#187; demeters revenge</title>
	<atom:link href="http://www.3hv.co.uk/blog/tag/demeters-revenge/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3hv.co.uk/blog</link>
	<description>precision engineering for your website</description>
	<lastBuildDate>Fri, 13 Jan 2012 16:18:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The trouble with mocks (or design versus acceptance)</title>
		<link>http://www.3hv.co.uk/blog/2009/03/12/the-trouble-with-mocks-or-design-versus-acceptance/</link>
		<comments>http://www.3hv.co.uk/blog/2009/03/12/the-trouble-with-mocks-or-design-versus-acceptance/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 05:26:02 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Bug-Free Code]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[demeters revenge]]></category>
		<category><![CDATA[luke redpath]]></category>
		<category><![CDATA[mock objects]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[shoulda]]></category>
		<category><![CDATA[specifying]]></category>
		<category><![CDATA[Writing Reliable]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=426</guid>
		<description><![CDATA[I had the pleasure of speaking to Luke Redpath the other day. I started off by thanking him for his Demeter&#8217;s Revenge plugin, which is one of the first things I install on a new project. He said he didn&#8217;t use it much any more, as he doesn&#8217;t do mocking, except during the design process. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the pleasure of speaking to <a href="http://www.lukeredpath.co.uk/">Luke Redpath</a> the other day.  </p>
<p>I started off by thanking him for his <a href="http://www.lukeredpath.co.uk/blog/demeters-revenge.html">Demeter&#8217;s Revenge</a> plugin, which is one of the first things I <a href="http://github.com/caius/demeters_revenge/tree/master">install</a> on a new project. He said he didn&#8217;t use it much any more, as he doesn&#8217;t do mocking, except during the design process.  This surprised me, but thinking about it, the distinction between what your different types of tests <em>are for</em> is an important one.  </p>
<p>The most important part of using mocks is their value in designing your class&#8217;s public API.  You start with a <a href="http://cukes.info/">cucumber</a> feature (your acceptance test) and as you are implementing it, fill in specs for each component in turn.  As features are defined in terms of the user interface, you start by specifying and designing your view and controller.  The controller needs to interact with a model.  And this is where mocking comes into its own.  </p>
<pre><code>
describe WotsitPokesController
  it "should poke a wotsit" do
    on_posting_to :create, :id => '1' do
      @wotsit = mock_model Wotsit
      Wotsit.should_receive(:find).with('1').and_return(@wotsit)
      @wotsit.should_receive(:poke).and_return(true)
    end
    response.should redirect_to(edit_wotsit_path(@wotsit))
    flash[:notice].should == 'Your wotsit has been poked'
  end
</code></pre>
<p>(this uses the <a href="http://github.com/brightbox/rspec-rails-extensions/tree/master">RSpec-Rails Extensions</a> to tidy up the specification)</p>
<p>Whatever @wotsit.poke does, when you are mocking, it is in your interests to encapsulate its behaviour within a single method.  Otherwise you end up writing tons of &#8220;fake&#8221; code within your spec, which makes the tests brittle and hard to maintain.  </p>
<p>A small housekeeping note; as soon as your controller spec references @wotsit.poke, you need to go to your model spec and add:</p>
<pre><code>
  it "should poke itself"
</code></pre>
<p>A pending specification, just as a reminder that you&#8217;ve got a method to implement in a few minutes time.  </p>
<p>But Luke had started doing full-stack testing alone; using <a href="http://thoughtbot.com/projects/shoulda/">shoulda</a> (with its nested contexts) to write acceptance tests, rather than <a href="http://rspec.info">RSpec</a> and mocking.  </p>
<p>I&#8217;m not so sure about this. His reason was the brittleness of mocked tests.  Agreed, it can be dangerous; if you rename the &#8220;poke&#8221; method on your Wotsit, your controller spec will still pass.  <strong>But</strong>, crucially, your cucumber story (or shoulda integration test) will not.  </p>
<p>So I guess the point to make here is that specifying with mocks alone can be a <em>bad idea</em>.  You need a full-stack test to catch the stuff that &#8220;falls between the cracks&#8221;, the proof that your application does what it is supposed to do.  </p>
<p>Can you just forego the mocks completely?  I don&#8217;t think so; they are too valuable during the exploration/design phase.  </p>
<p>Should you just get rid of the mocks once your acceptance test is passing?  Luke says yes.  I&#8217;m still undecided, but certainly am tending towards no.  </p>
<p>So, what&#8217;s your take?  Are mocks too brittle to be used in long term development?  Or, coupled with an acceptance test, is the value they give so great we need to keep them in our test suite?  </p>
<p>(interestingly, since I drafted this post, Pat Maddox has also <a href="http://www.patmaddox.com/blog/you-probably-dont-get-mocks">written about much the same subject with a similar outcome</a> &#8211; including Matt Wynne stating &#8220;<cite>Acceptance tests are for regression coverage, unit tests are for design</cite>&#8221; in the comments, although Pat himself says you can delete the specs once the design is done).  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/03/12/the-trouble-with-mocks-or-design-versus-acceptance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.203 seconds -->

