<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

  <channel>
  	<title>Putting it together</title>
  	<link>http://blog.lmcavalle.com</link>
  	<description>Bit by bit...</description>
	
  	
  	
  	<creativeCommons:license>http://creativecommons.org/licenses/by/2.0/</creativeCommons:license><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/PuttingItTogether" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
  		<title>Structure-based testing</title>
  		<link>http://feedproxy.google.com/~r/PuttingItTogether/~3/7E_avKoBwjI/structure-based-testing.html</link>
  		<pubDate>2009-03-18T00:00:00-07:00</pubDate>
  		<dc:creator>Luismi Cavallé</dc:creator>
  		<guid isPermaLink="false">http://cavalle.github.com/2009/03/18/structure-based-testing</guid>		
  		<content:encoded><![CDATA[
  		  <p>We already knew that there were two styles of testing: <em>state-based</em> and <em>interaction-based</em>. The former consists of specifying how the state of an object should change after exercising its methods. As for the latter, the interaction between the object and its collaborators is what is specified. Martin Fowler <a href='http://martinfowler.com/articles/mocksArentStubs.html'>distinguishes</a> between the users of each style as classicists (state-based) and mockists (interaction-based), because of the tool needed to perform this style of testing, known as mocks.</p>

<p>Similarly to mocking, which is a technique that enables a style of testing different to the classical, we could begin to talk of a testing style made possible by the use of macros and call it <em><em>structure-based testing</em></em>.</p>

<p>I&#8217;ve <a href='http://www.lmcavalle.com/2008/01/21/una-verdad-incomoda/'>already blogged</a> about Shoulda and its macros, but given the recent news about <a href='http://giantrobots.thoughtbot.com/2009/2/3/speculating-with-shoulda'>the Shoulda - RSpec love affair</a> and the <a href='http://github.com/carlosbrando/remarkable'>remarkable</a> increasing popularity of Shoulda-like macros, I reckon it’s time to revisit the subject.</p>

<p>I find appropriate to call this style of testing <em>structure-based</em> because it doesn’t involve the interactions nor the changes of the state, but the actual structure of the object under test. In that sense, structure means the opposite of behaviour. These tests don&#8217;t say anything about the behaviour of the object, just what it should look like.</p>
<div class='small_code'>

<div class='highlight'><pre><span class='n'>describe</span> <span class='no'>User</span> <span class='k'>do</span>                             <span class='c1'># class User &lt; ActiveRecord::Base</span>
  <span class='n'>it</span> <span class='p'>{</span> <span class='n'>should</span> <span class='n'>belong_to</span><span class='p'>(</span><span class='ss'>:account</span><span class='p'>)</span> <span class='p'>}</span>          <span class='c1'>#   belongs_to :account          </span>
  <span class='n'>it</span> <span class='p'>{</span> <span class='n'>should</span> <span class='n'>have_many</span><span class='p'>(</span><span class='ss'>:posts</span><span class='p'>)</span> <span class='p'>}</span>            <span class='c1'>#   have_many :posts             </span>
  <span class='n'>it</span> <span class='p'>{</span> <span class='n'>should</span> <span class='n'>validate_presence_of</span><span class='p'>(</span><span class='ss'>:email</span><span class='p'>)</span> <span class='p'>}</span> <span class='c1'>#   validate_presence_of :email  </span>
<span class='k'>end</span>                                          <span class='c1'># end                            </span>
</pre></div>

</div>
<p>The thing is, I think there&#8217;s hardly any value in this kind of testing. Obviously these tests are easy to write and quick to run. The cost of writing them is low so the benefit could also be low and still be a good investment. However, there is usually an almost exact correspondence between the spec and the implementation. It would be possible to automatically generate one from the other. I can see some remote value in thinking twice about the structure of the object, but that won&#8217;t give me any clue about whether that structure will provide the behaviour I need to implement the current functionality. I&#8217;m just writing the same thing twice, but with different languages.</p>
<center>
  <img src='/images/satisfaction.jpg' alt='Satisfaction Guaranteed' />
</center>
<p>Unit-testing Rails applications is not straightforward. Dan North <a href='http://www.nabble.com/RSpec-makes-me-want-to-write-better-code-tt19671383.html#a19705251'>justifies this</a> by blaming the framework itself and the coupled nature of its components. Shoulda macros gives you a shortcut, they &#8220;make tests easy on the fingers and eyes&#8221;, that&#8217;s the framework&#8217;s motto. And they certainly provide what they promise: instant gratification, effortless testing with high code coverage and relatively fast execution time you can perform &#8220;all the fucking time&#8221;, like the cool kids do. The question is, does it provide any real value to the development process?</p>

<p>The answer depends, of course. It depends on each developer, team and context. <a href='http://www.lmcavalle.com/2009/02/10/how-we-do-bdd.html'>My current approach</a> of Rails testing is pretty different. But that might very well change in the future. The success probably lies in continuously challenging the why and the how of the way you do things, keeping in mind that there is no silver bullet and that, ultimately, it&#8217;s all about effectively building software that provides value to someone.</p>
  		<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/PuttingItTogether?a=7E_avKoBwjI:VqeNi08f3uU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/PuttingItTogether?d=7Q72WNTAKBA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PuttingItTogether/~4/7E_avKoBwjI" height="1" width="1"/>]]></content:encoded>
  	<feedburner:origLink>http://www.lmcavalle.com/2009/03/18/structure-based-testing.html</feedburner:origLink></item>
    
  	
  	
  	<item>
  		<title>How we do BDD</title>
  		<link>http://feedproxy.google.com/~r/PuttingItTogether/~3/Nr9N7Ko-9DA/how-we-do-bdd.html</link>
  		<pubDate>2009-02-10T00:00:00-08:00</pubDate>
  		<dc:creator>Luismi Cavallé</dc:creator>
  		<guid isPermaLink="false">http://cavalle.github.com/2009/02/10/how-we-do-bdd</guid>		
  		<content:encoded><![CDATA[
  		  <p>Since I believe we also have something to say about <a href='http://delicious.com/cavalle/ruby_testing_fuss'>all the fuss involved in testing</a>, here is a 15-point summary on how we do it at <a href='http://bebanjo.com'>BeBanjo</a>, my current employer, as it was presented at Conferencia Rails 2008:</p>

<ol>
<li>We perform testing on two different levels: <strong>Acceptance and Unit</strong>.</li>

<li><strong>For Acceptance Testing we use Cucumber</strong> (formerly Story Runner). Cucumber automatically fills the gap between a user story written in plain English and an executable test.</li>

<li><strong>For Unit Testing we use RSpec</strong>. We could also use <em>Shoulda</em>, <em>Context</em>+<em>Matchy</em> or even <em>Test:Unit</em>+<em>Mocka</em>. Any of these would do. We believe it’s a matter of taste, essentially you can do the same with each of them.</li>

<li>We find Acceptance Testing <strong>excellent as a verification tool</strong>, but <strong>not as good as a design/development tool</strong>. </li>

<li>We therefore need <strong>Unit Testing as a development/design tool</strong> to enable us to be more productive and to make our code more maintainable. </li>

<li>We follow an <strong>outside-in approach driven by the user story</strong>. We try to write the minimal code necessary to make the next step pass. From the view to the model, outside-in, adding the corresponding specs on the way.</li>

<li><strong>A comprehensive set of acceptance tests</strong> is essential to the way we do specs. If we didn’t have it, we would unit-test in a completely different way.</li>

<li>We understand that <a href='http://martinfowler.com/articles/mocksArentStubs.html'>&#8220;Mocks aren’t stubs&#8221;</a> concludes that there’s no winner between classicists and mockists. <strong>It’s all about trade-offs</strong>, didn’t you know?</li>

<li>We only spec <strong>the interesting behaviour</strong> of the object under test, the behaviour that other parts of the system rely on.</li>

<li>We <strong>don’t usually spec views</strong>. We think it is unnecessary with acceptance testing.</li>

<li>We spec <strong>controllers following an interaction-based approach</strong> using mocks. Controllers have no state, their interesting behaviour is determined by the way they interact with other objects (models and views). That’s why we think interaction-based testing is the most appropriate for controllers.</li>

<li>We spec <strong>models following a state-based approach</strong>. And Rails fixtures are evil. Instead we use any implementation of the Factory pattern (<a href='http://replacefixtures.rubyforge.org/'>fixture-replacement</a>, <a href='http://www.thoughtbot.com/projects/factory_girl'>FactoryGirl</a>, <a href='http://github.com/notahat/machinist/tree/master'>machinist</a>&#8230;)</li>

<li>We <strong>don’t use macros in our specs</strong>. Macros encourage a style of testing I call <em>structure-based</em> (vs. state-based or interaction-based), which, in my opinion, gives almost no value. Using them feels like writing the same thing twice. Perhaps because that’s exactly what you’re doing.</li>

<li><strong>BDD works better when accompanied by other agile practices</strong>: pair-programming, refactoring, interface-first, short iterations, etc. </li>

<li><strong>We are opinionated</strong>, like our favourite web framework.</li>
</ol>

<p>You can find the slides of the presentation <a href='http://www.slideshare.net/jjggss/bdd-from-the-trenches-presentation'>here</a> (in English) and the video (in Spanish) <a href='http://video.google.com/videoplay?docid=-6091603113187386819'>here</a>.</p>

<p>I’m working on a series of posts to elaborate on each of these points. Stay tuned!</p>
  		<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/PuttingItTogether?a=ElMnzUok"><img src="http://feeds.feedburner.com/~f/PuttingItTogether?d=50" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/PuttingItTogether/~4/Nr9N7Ko-9DA" height="1" width="1"/>]]></content:encoded>
  	<feedburner:origLink>http://www.lmcavalle.com/2009/02/10/how-we-do-bdd.html</feedburner:origLink></item>
    
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	

	</channel>
	
</rss>
