<?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; rspec</title>
	<atom:link href="http://www.3hv.co.uk/blog/tag/rspec/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>&#8211;format=specdoc</title>
		<link>http://www.3hv.co.uk/blog/2011/02/24/formatspecdoc/</link>
		<comments>http://www.3hv.co.uk/blog/2011/02/24/formatspecdoc/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 09:00:17 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[details]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=807</guid>
		<description><![CDATA[One of the greatest things about RSpec is that the specifications you write not only prove that your code is doing what it should be doing, but also it provides a starting point for your technical documentation. Which means you are secure enough in your development to add functionality like this: ApplicationHelper greeting - should [...]]]></description>
			<content:encoded><![CDATA[<p>One of the greatest things about RSpec is that the specifications you write not only prove that your code is doing what it should be doing, but also it provides a starting point for your technical documentation.  </p>
<p>Which means you are secure enough in your development to add functionality like this: </p>
<pre>
ApplicationHelper greeting
- should say 'morning' between 4am and 12pm
- should say 'afternoon' between 12pm and 5pm
- should say 'evening' between 5pm and 9pm
- should say 'night' between 9pm and 4am
</pre>
<p>One of those <a href="http://www.3hv.co.uk/blog/2007/05/25/great-features-you-see-great-features-you-dont/">tiny details</a> that most people won&#8217;t even notice &#8211; but makes a subliminal impression on your users (in this case, greeting them with a &#8220;Good morning&#8221; or &#8220;Good evening&#8221; depending upon the time).  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2011/02/24/formatspecdoc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Switching off transactions for a single spec when using RSpec</title>
		<link>http://www.3hv.co.uk/blog/2009/05/08/switching-off-transactions-for-a-single-spec-when-using-rspec/</link>
		<comments>http://www.3hv.co.uk/blog/2009/05/08/switching-off-transactions-for-a-single-spec-when-using-rspec/#comments</comments>
		<pubDate>Fri, 08 May 2009 10:44:28 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[Bug-Free Code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[transactions]]></category>
		<category><![CDATA[Writing Reliable]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=503</guid>
		<description><![CDATA[I have just written a load of test code that needed to verify that a particular set of classes behaved correctly when a transaction was rolled back. However, the rest of my suite relied on transactional fixtures (which is Rails&#8217; badly named way of saying that a transaction is started before each test and rolled [...]]]></description>
			<content:encoded><![CDATA[<p>I have just written a load of test code that needed to verify that a particular set of classes behaved correctly when a transaction was rolled back.  </p>
<p>However, the rest of my suite relied on transactional fixtures (which is Rails&#8217; badly named way of saying that a transaction is started before each test and rolled back at the end, leaving your test database in a pristine state before the next case is run). </p>
<p>In particular, my spec_helper.rb had the following:</p>
<pre>
Spec::Runner.configure do |config|
  config.use_transactional_fixtures = true
  # stuff
end
</pre>
<p>The code being tested looked something like this: </p>
<pre>
begin
  Model.transaction do
    do_something # may fail with an exception
    do_something_else # may fail with an exception
  end
rescue Exception => ex
  do_some_recovery_stuff
end
</pre>
<p>I had a spec for the successful path (checking that the outcomes of <tt>do_something</tt> and <tt>do_something_else</tt> were what I expected.  </p>
<p>However when I tried the same for the failure paths, the outcomes matched the successful path.  The time-tested debugging method of sticking some <tt>puts</tt> statements in various methods showed that <tt>do_some_recovery_stuff</tt> was being called as expected.  But the outcomes were still wrong.  </p>
<p>And the reason?  Transactions.  This was a Rails 2.2 project, running on Mysql (innodb).  As RSpec/Test::Unit starts a transaction before the specification clause runs (and then rolls it back on completion) when <tt>Model.transaction</tt> statement is reached, the spec is actually starting a second transaction, nested within RSpec/Test:Unit&#8217;s.  Which means when the inner transaction is rolled back, the database doesn&#8217;t actually do anything &#8211; there&#8217;s still an outer transaction that may or may not be rolled back.  (I think Rails 2.3 corrects this behaviour and if you roll back an inner transaction then the outer transaction reflects the correct state, but I&#8217;m not 100% on that).  </p>
<p>So I had a choice &#8211; move the (production) app to Rails 2.3 to fix this one bug (which is very urgent) or figure out how to switch the outer transaction off for these particular steps.  Google wasn&#8217;t very helpful &#8211; lots of stuff on how RSpec extends Test::Unit, lots of stuff on how Rails extends Test::Unit to add the fixtures (and transaction) support.  But no concrete example on how to actually switch it off. </p>
<p>After much playing around (overriding methods like <tt>use_transaction?(method_name)</tt> and <tt>runs_in_transaction?</tt>) I eventually stumbled across the answer.  And it&#8217;s pretty simple.  </p>
<p>Set your default to be <tt>config.use_transactional_fixtures = true</tt> in spec_helper.rb.  Then, for the specs that are not transactional, simply create a <tt>describe</tt> block and simply add the following: </p>
<pre>
describe MyClass, "when doing its stuff" do
  self.use_transactional_fixtures = false
  it "should do this"
  it "should do that"
  it "should do the other"
end
</pre>
<p>The only thing to be aware of is you may well need an <tt>after :each</tt> block to clean up after yourself.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/05/08/switching-off-transactions-for-a-single-spec-when-using-rspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing bugs in untested code</title>
		<link>http://www.3hv.co.uk/blog/2009/05/06/fixing-bugs-in-untested-code/</link>
		<comments>http://www.3hv.co.uk/blog/2009/05/06/fixing-bugs-in-untested-code/#comments</comments>
		<pubDate>Wed, 06 May 2009 20:54:19 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[bug-fixing]]></category>
		<category><![CDATA[Bug-Free Code]]></category>
		<category><![CDATA[legacy code]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[Writing Reliable]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=493</guid>
		<description><![CDATA[When you&#8217;ve got an application that has little or no test coverage it can be quite daunting making changes. What if you alter X and it breaks Y? Without running through the entire app by hand how will you know what you&#8217;ve broken? Well you won&#8217;t. Even worse, what if your client reports a bug [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;ve got an application that has little or no test coverage it can be quite daunting making changes.  What if you alter X and it breaks Y?  Without running through the entire app by hand how will you know what you&#8217;ve broken?  </p>
<p>Well you won&#8217;t.  </p>
<p>Even worse, what if your client reports a bug in that application?  That makes things even worse doesn&#8217;t it? </p>
<p>No.  It&#8217;s actually an opportunity.  Because even if your boss thinks &#8220;testing is a great idea, we&#8217;ll start on the next project when we&#8217;ve got more time&#8221;, a bug fix is one of those things where the time to fix varies.  So take advantage of that.  </p>
<p>Start by writing a test that reproduces some functionality in the same area that you know works.  </p>
<p>This won&#8217;t be easy.  You need to get the database into the correct state, set up the session correctly.  To save you some time, try using an <a href="http://wiki.github.com/brightbox/object-factory/usage-with-rails-rspec-and-cucumber">object factory</a> &#8211; with the correct configuration you can concentrate on creating just the models you need without having to fill the entire database with test data.  </p>
<p>Take a look at the code you are testing as you are writing the tests.  But make sure your test checks the outcomes of that code, not the implementation &#8211; when using mocks it&#8217;s pretty easy to end up effectively rewriting the real code as a series of <tt>should_receive(:something)</tt> calls.  Which looks great until you come to refactor, at which point it becomes a nightmare. </p>
<p>Get your test to pass.  Remember this is a feature that works so it shouldn&#8217;t be <em>that</em> hard to get it to pass.  And you are building some important foundations as you are setting up a configuration for your object factory a model at a time.    </p>
<p>Once you&#8217;ve got the first test working, prove that it&#8217;s doing what it&#8217;s supposed to be doing.  Comment out its of the implementation and watch it fail.  If it doesn&#8217;t fail then you&#8217;ve got a problem &#8211; your test is testing something other than that particular implementation.  </p>
<p>Now we&#8217;ve got a test that really checks your existing code.  I like to add another test that checks the error handling in that existing code as well (there is error handling in your existing code isn&#8217;t there?)  Follow the same process as before &#8211; test the outcomes (probably catching an exception if you&#8217;re at the model level, probably looking for a particular redirect and flash message at the controller level), and then prove that it works by commenting code out.  Hopefully this should be pretty quick to write as most of the hard work was done when setting up the original test.  </p>
<p>And finally we can get to the meat of it.  Write a test that reproduces your bug (that is, your test exercises your app in the way expected and your app should fail).  Again, most of your setup work should already be done, so it shouldn&#8217;t take too long.  Now run the test and watch it fail.  If it doesn&#8217;t fail then your test isn&#8217;t right.  </p>
<p>After all of this, we are finally in a position to fix the bug.  </p>
<p>Your test should prove that it&#8217;s been fixed.  And prove that the bug won&#8217;t reappear in a future version.  But you&#8217;ve also taken an important first step to wrapping your application in tests, making your life easier in future.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/05/06/fixing-bugs-in-untested-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Acceptance Testing in Ruby, Rails, RSpec and Cucumber</title>
		<link>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/</link>
		<comments>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 18:16:13 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=328</guid>
		<description><![CDATA[I&#8217;ve written up a new post at the Brightbox blog detailing how we are using RSpec and Cucumber to build acceptance tests for the next generation Brightbox systems.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written up a new post at the <a href="http://blog.brightbox.co.uk/posts/using-rspec-cucumber-and-user-stories-to-build-our-internal-systems">Brightbox blog</a> detailing how we are using <a href="http://rspec.info/">RSpec</a> and <a href="http://github.com/aslakhellesoy/cucumber/tree/master">Cucumber</a> to build acceptance tests for the next generation Brightbox systems.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

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

