Nick Sieger: RubyConf Day 3: Behaviour-Driven Development with RSpec http://blog.nicksieger.com/articles/2007/11/04/rubyconf-day-3-behaviour-driven-development-with-rspec en-us 40 RubyConf Day 3: Behaviour-Driven Development with RSpec <h2>David Chelimsky and Dave Astels: RSpec</h2> <div class="typocode"><pre><code class="typocode_ruby "><span class="ident">describe</span> <span class="constant">TestDriverDevelopment</span> <span class="keyword">do</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">is an incremental process</span><span class="punct">&quot;</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">drives the implementation</span><span class="punct">&quot;</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">results in an exhaustive test suite</span><span class="punct">&quot;</span> <span class="comment"># but also...</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">should focus on design</span><span class="punct">&quot;</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">should focus on documentation</span><span class="punct">&quot;</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">should focus on behaviour</span><span class="punct">&quot;</span> <span class="keyword">end</span> <span class="keyword">class </span><span class="class">BehaviourDrivenDevelopment</span> <span class="punct">&lt;</span> <span class="constant">TestDrivenDevelopment</span> <span class="ident">include</span> <span class="constant">FocusOnDesign</span> <span class="ident">include</span> <span class="constant">FocusOnDocumentation</span> <span class="ident">include</span> <span class="constant">FocusOnBehavior</span> <span class="keyword">end</span></code></pre></div> <p>When doing test&#45;driven development:</p> <ul> <li>Write your intent first&#46; The smallest test you can that fails&#46;</li> <li>Next, write the implementation&#46; The simplest thing that could possibly work&#46;</li> <li>Even though you may be tempted to think about additional edge cases, multiple requirements, etc&#46;, you should try to be disciplined and focus only on the immediate tests&#46; Only after you&#8217;ve made one test fail, then pass, can you continue on to other tests&#46;</li> </ul> <h3>RSpec history</h3> <p>Initially BDD was just a discussion among Aslak Hellesoy and Dan North in the ThoughtWorks London office&#46; Dave Astels joined the conversation with a blog post stating that he thought these ideas could be easily implemented in Smalltalk or Ruby&#46; Steven Baker jumped in with an initial implementation, and released RSpec 0&#46;1&#46; Later in 2006, maintenance was handed over to David Chelimsky&#46; RSpec has evolved through a dog&#45;fooding phase up to the present 1&#46;0 product&#46;</p> <p>BDD is no longer just about &#8220;should instead of assert&#8221;, it&#8217;s evolving into a process&#46; Emphasizing central concepts from extreme programming and domain&#45;driven design, it&#8217;s moving toward focusing on customer stories and acceptance testing&#46; It&#8217;s outside&#45;in, starting at high levels of detail, rather than low&#45;level like RSpec or Test::Unit&#46;</p> <h3>Story Runner</h3> <p>Story Runner is a new feature intended for RSpec 1&#46;1&#46; Each story is supposed to capture a customer requirement in the following general template:</p> <pre><code>As a (role) ... I want to (some function) ... so that (some business value). </code></pre> <p>It uses a &#8220;Scenario &#46;&#46;&#46; Given &#46;&#46;&#46; When &#46;&#46;&#46; Then &#46;&#46;&#46;&#8221; format to express the high level stories&#46; Scenarios are a series of given items, steps, and behaviour validations&#46; Once the basic steps are established, they can be re&#45;used&#46; David even demonstrated a preview of an in&#45;browser story runner that would allow the customer to play with the implementation and create new scenarios&#46;</p> <h3>Pending</h3> <p><em>Pending</em> is a nice way to mark specs as &#8220;in&#45;progress&#8221;&#46; You can either omit a block for your spec, or use <code>pending</code> inside the block to leave a placeholder to come back to&#46;</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="ident">describe</span> <span class="constant">Pending</span> <span class="keyword">do</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">doesn't need a block to be pending</span><span class="punct">&quot;</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">could also be specified inside the block</span><span class="punct">&quot;</span> <span class="keyword">do</span> <span class="ident">pending</span><span class="punct">(&quot;</span><span class="string">TODO</span><span class="punct">&quot;)</span> <span class="ident">this</span><span class="punct">.</span><span class="ident">should_not</span> <span class="ident">be_a_failure</span> <span class="keyword">end</span> <span class="ident">it</span> <span class="punct">&quot;</span><span class="string">could also use a block with pending, and you will be notified when it starts to succeed</span><span class="punct">&quot;</span> <span class="keyword">do</span> <span class="ident">pending</span><span class="punct">(&quot;</span><span class="string">TODO</span><span class="punct">&quot;)</span> <span class="keyword">do</span> <span class="ident">this</span><span class="punct">.</span><span class="ident">should_not</span> <span class="ident">be_a_failure</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="keyword">end</span></code></pre></div> <p>Behaviour&#45;Driven Development in Ruby with RSpec is a new book David and Aslak are working on, due out early next year&#46;</p> <p><em>Update: <a href="http://blog.davidchelimsky.net/articles/2007/11/05/rubyconf-slides">David has posted his slides</a>&#46;</em></p> Sun, 04 Nov 2007 16:26:00 +0000 urn:uuid:48613cce-c252-4be0-a12e-923eb239c87a Nick Sieger http://blog.nicksieger.com/articles/2007/11/04/rubyconf-day-3-behaviour-driven-development-with-rspec rubyconf rubyconf2007 rspec http://blog.nicksieger.com/articles/trackback/343