Skip to content


Stumbling in to RSpec and Behavior Driven Development

Background

<p>A few years back, when I was still hacking away at Perl and Java, I discovered the wonderful world of testing.  At first, it was hard to get my tests to work but with diligence and lots of time, I was able to get something up and going.  The problem was that I was writing bad brittle tests.  My technique involved massaging some code into something usable and then writing a test to prove that code worked.  What usually ended up happening is that I would write lots of tests, and when I changed something in the code, I would have to go back and update my tests because they were too attached to implementation.  I always had a feeling things could be better, but could never find anything to satisfy my tastes.</p>


<p>Two years ago when I made the commitment to developing in Ruby and Rails as much as possible, I revisited my testing style.  Once again, I found that my tests were brittle and weren&#8217;t very useful because of the time it took to maintain them.  Once again, I had a feeling things could be better, but I wasn&#8217;t sure what better was.</p>


<p>Now, I&#8217;m using RSpec (and Test::Unit when I have to), and I can say things are much better.  I&#8217;m writing examples (and tests) first, because doing otherwise feels very unnatural.  So, what happened in the last two years?</p>


<h2>The Evolution Begins</h2>


<p>In August 2006, I started a new project designing a site for a local business from scratch.  The project wasn&#8217;t something I was intimately familiar with, so I decided to start outlining my code with Test::Unit tests to describe the functionality.  Little did I know, that would start the journey to where I am today.  The tests I wrote weren&#8217;t that good, but that they allowed me to visualize the system before writing much code, and because of that, I was able to act quickly and have a fast turn around.  At that point, I thought I was on to something, because for the first time Test Driven Development (TDD) had finally made sense to me.</p>


<h2>The Evolution Continues: Enter RSpec</h2>


<p>Which with some time and a considerable amount of code later I was able to efficiently write tests first.  And that was good.</p>


<p>I don&#8217;t remember the first time I heard about RSpec, or the first time I saw its crazy syntax.  I don&#8217;t even remember the first piece of code I tried to use with RSpec.  I do remember the Eureka Moment I had with RSpec.  One problem I&#8217;ve always had with Test::Unit and Rails is test organization.  The <em>context</em> aspect of RSpec is what made it so appealing.  Finally I was able to organize my tests in a way that visually appealing and at the same time saving.  After switching to RSpec for my tests, I was still coding them the same way I would Test::Unit.  I was just using a different syntax.</p>


<h2>RSpec Reloaded</h2>


<p>After studying the RSpec mailing list for a while, I became more and more curious with the notion of Behavior Driven Development (BDD).  At first, I thought it was a whole bunch of hocus pocus voodoo, but I was determined to figure it all out, so I stuck with it.  I stuck with it for a while, even with it was uncomfortable.  And then it hit me.</p>


<h2>Behavior Driven Development&#8212;The Legend</h2>


<blockquote>
    <p> Describe what the code should do, rather than what it does.</p>
</blockquote>


<p>You might think you are already doing this.  And truthfully, you might be.  But mostly likely you aren&#8217;t, because it isn&#8217;t obvious.  (At least it wasn&#8217;t obvious to me).  <span class="caps">BDD</span> is <span class="caps">TDD</span> to me.  I learned to describe behaviors rather describe code.</p>


<p>Make sure to come see me at <a href="http://www.actsasconference.com/speakers/bryan-liles">Acts As Conference</a> next month.</p>

Posted in Uncategorized.