<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Random Hacks: Designing programs with RSpec and Cucumber (plus a book recomendation)</title>
    <link>http://www.randomhacks.net/articles/2009/04/30/rspec-cucumber-book-recommendation</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"Designing programs with RSpec and Cucumber (plus a book recomendation)" by Eric</title>
      <description>&lt;p&gt;Cobol, I think, pretty much has to be reckoned as a total failure.&lt;/p&gt;


	&lt;p&gt;And in general, I dislike &amp;#8220;natural language&amp;#8221; programming tools. I &lt;i&gt;still&lt;/i&gt; loathe AppleScript, for example, because I could never guess when the parser wanted me to include a &amp;#8220;the&amp;#8221; token.&lt;/p&gt;


	&lt;p&gt;On the hand, I know at least one Scheme hacker who loves &lt;a href="http://www.inform-fiction.org/I7/Gallery%204-5.html"&gt;Inform&lt;/a&gt;. And since I respect his taste in matters of natural language parsing, I suspect that Inform must be at least modestly amusing.&lt;/p&gt;


	&lt;p&gt;Cucumber succeeds, I think, because it&amp;#8217;s not even remotely a programming language. It&amp;#8217;s just a bunch of regex matchers wired up to Webrat. You can&amp;#8217;t loop, you can&amp;#8217;t declare variables, and you certainly can&amp;#8217;t call subroutines. Really, all you can do is write heavily stylized documentation, and verify it against your program. And on this level, weirdly, it works.&lt;/p&gt;


	&lt;p&gt;I try very hard to not judge programming languages too quickly. I mean, at first glance, Haskell looks like a deliberately crippled academic toy, Ruby looks like warmed-over Python, and Lisp looks like (in Larry Wall&amp;#8217;s immortal worlds) &amp;#8220;a bowl of oatmeal full of finger clippings.&amp;#8221; But I would have a lot less fun programming if I hadn&amp;#8217;t taken the time to get past those first impressions.&lt;/p&gt;</description>
      <pubDate>Sat, 02 May 2009 00:55:15 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:25c6490f-5120-44b0-b287-1f071a6f6527</guid>
      <link>http://www.randomhacks.net/articles/2009/04/30/rspec-cucumber-book-recommendation#comment-614</link>
    </item>
    <item>
      <title>"Designing programs with RSpec and Cucumber (plus a book recomendation)" by Bryan O'Sullivan</title>
      <description>&lt;p&gt;Oh wow. I never knew that Rubyists had a Cobol fetish. I think I liked my brain better when it didn&amp;#8217;t contain that knowledge.&lt;/p&gt;</description>
      <pubDate>Fri, 01 May 2009 17:24:54 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c6ff5cbd-e00c-42c3-aa20-9dd6d1a20424</guid>
      <link>http://www.randomhacks.net/articles/2009/04/30/rspec-cucumber-book-recommendation#comment-612</link>
    </item>
    <item>
      <title>Designing programs with RSpec and Cucumber (plus a book recomendation)</title>
      <description>&lt;p&gt;Over the last couple of years, I&amp;#8217;ve occasionally written Ruby programs using &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt; and (more recently) &lt;a href="http://rspec.info/"&gt;Cucumber&lt;/a&gt;. These two tools are inspired by &lt;a href="http://www.amazon.com/Test-Driven-Development-Addison-Wesley-Signature/dp/0321146530"&gt;Test Driven Development&lt;/a&gt; (TDD), a school of thought which says you should write unit tests &lt;em&gt;before&lt;/em&gt; implementing a feature. &lt;/p&gt;

&lt;p&gt;When doing TDD, you work inwards from the interface to the implementation. You start by writing a test case against the interface you &lt;em&gt;wish&lt;/em&gt; you had, and then you make that test case work. This is a subtle shift in how you approach a design problem, but it frequently results in beautiful APIs. (And you also get a fully automated test suite for your software, liberating you to make much larger changes without fear of breaking things.)&lt;/p&gt;

&lt;h3&gt;The problem with the word &amp;#8220;test&amp;#8221;&lt;/h3&gt;

&lt;p&gt;Unfortunately, the name &amp;#8220;Test Driven Development&amp;#8221; is misleading. Most folks think of &amp;#8220;testing&amp;#8221; as something you do &lt;em&gt;after&lt;/em&gt; development is complete. But TDD is really more of a design activity&amp;#8212;you&amp;#8217;re specifying how your APIs should work &lt;em&gt;before&lt;/em&gt; you actually start coding.&lt;/p&gt;

&lt;p&gt;Dan North spent some time struggling to teach developers about TDD. After a while, he decided that the main barrier to understanding was the word &amp;#8220;test.&amp;#8221; He proposed replacing TDD with &lt;a href="http://dannorth.net/introducing-bdd"&gt;Behavior Driven Development&lt;/a&gt; (BDD), and he started referring to unit tests as &amp;#8220;specifications.&amp;#8221;&lt;/p&gt;

&lt;p&gt;In the Ruby community, the most popular BDD tool is &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt;. Using RSpec, you might specify an API something like this:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;describe&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;simplify_name&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
  &lt;span class="ident"&gt;it&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;should convert all letters to lowercase&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
    &lt;span class="ident"&gt;simplify_name&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;AbC&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;).&lt;/span&gt;&lt;span class="ident"&gt;should&lt;/span&gt; &lt;span class="punct"&gt;==&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;abc&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="ident"&gt;it&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;should remove everything but letters and spaces&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
    &lt;span class="ident"&gt;simplify_name&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt; Joe Smith 3 -+&lt;span class="escape"&gt;\n&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;).&lt;/span&gt;&lt;span class="ident"&gt;should&lt;/span&gt; &lt;span class="punct"&gt;==&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;joe smith&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After writing this specification, you would then go ahead and implement &lt;code&gt;simplify_name&lt;/code&gt;. And from then on, whenever you changed your program, you could automatically check it against this specification.&lt;/p&gt;

&lt;h3&gt;Using specifications to communicate with clients and users&lt;/h3&gt;

&lt;p&gt;By itself, RSpec is mostly useful for programmers. Sure, a specification looks a lot like English. But would you really want to show it to an end user?&lt;/p&gt;

&lt;p&gt;&lt;a href="http://rspec.info/"&gt;Cucumber&lt;/a&gt; goes one step further. Instead of using code to specify how an API should work, it uses plain text to describe how a &lt;em&gt;user interface&lt;/em&gt; should work. For example:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_cucumber "&gt;Feature: Log in and out
  As an administrator
  I want to restrict access to certain portions of my site
  In order to prevent users from changing the content

  Scenario: Logging in
    Given I am not logged in as an administrator
    When I go to the administrative page
    And I fill in the fields
      | Username | admin  |
      | Password | secret |
    And I press &amp;quot;Log in&amp;quot;
    Then I should be on the administrative page
    And I should see &amp;quot;Log out&amp;quot;

  Scenario: Logging out
    ...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;#8217;s the neat part: This specification is actually an executable program. Each line of text corresponds to a &amp;#8220;step&amp;#8221;, which is defined in another file. Here&amp;#8217;s an example from the standard &lt;code&gt;webrat_steps.rb&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;Then&lt;/span&gt; &lt;span class="punct"&gt;/^&lt;/span&gt;&lt;span class="constant"&gt;I&lt;/span&gt; &lt;span class="ident"&gt;should&lt;/span&gt; &lt;span class="ident"&gt;see&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;([^&lt;span class="escape"&gt;\&amp;quot;&lt;/span&gt;]*)&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="global"&gt;$/&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;text&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
  &lt;span class="ident"&gt;response&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;should&lt;/span&gt; &lt;span class="ident"&gt;contain&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;text&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Cucumber encourages you to think at a very high level, and to specify how different users will actually use your software. It&amp;#8217;s particularly helpful if you need to communicate between programmers and end-users.&lt;/p&gt;

&lt;h3&gt;My experiences with RSpec and Cucumber&lt;/h3&gt;

&lt;p&gt;I&amp;#8217;ve been using RSpec on and off for a couple of years now, and Cucumber since late last year. Initially, I found both tools fascinating, but also a bit frustrating. Both RSpec and Cucumber have very strong opinions about how you should write software. Now, I found those opinions very interesting, and I was quite happy to be influenced by the assumptions built into the tools. But every now and then, I would need to do something that the authors of RSpec and Cucumber hadn&amp;#8217;t anticipated, and I would inevitably wind up struggling to make things work.&lt;/p&gt;

&lt;p&gt;But recent versions of RSpec and Cucumber are richer and more flexible. They cover more important cases straight out of the box, and they&amp;#8217;re easier to customize. So I can finally recommend both tools for real-world projects: They&amp;#8217;ll still guide your thinking, but they should give you enough flexibility to handle the corner-cases.&lt;/p&gt;

&lt;h3&gt;The RSpec (and Cucumber) book&lt;/h3&gt;

&lt;p&gt;Unfortunately, the documentation for RSpec and Cucumber is scattered around the web, and there aren&amp;#8217;t enough online guides showing the best way to solve common problems.&lt;/p&gt;

&lt;p&gt;But the Pragmatic Press is working on &lt;a href="http://www.pragprog.com/titles/achbd/the-rspec-book"&gt;The RSpec Book&lt;/a&gt;, which contains a large section on Cucumber, and a walkthrough of a typical development session using Cucumber and RSpec.&lt;/p&gt;

&lt;p&gt;Currently, the RSpec book is available as a &amp;#8220;beta book&amp;#8221;. This is a downloadable, DRM-free PDF, with periodic updates throughout the publishing process.  Right now, between one-third and one-half of the chapters have been roughed in, and the book is already very useful.&lt;/p&gt;

&lt;p&gt;So if you&amp;#8217;re curious about &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt; and &lt;a href="http://rspec.info/"&gt;Cucumber&lt;/a&gt;, have a look around the two web sites, and maybe watch some of the screencasts. If you decide to investigate further, pick up the beta book and dive in.&lt;/p&gt;</description>
      <pubDate>Thu, 30 Apr 2009 15:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:6e7a670a-6d09-430d-b788-da8135f1d8e2</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2009/04/30/rspec-cucumber-book-recommendation</link>
      <category>Ruby</category>
      <category>RSpec</category>
      <category>Cucumber</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/610</trackback:ping>
    </item>
  </channel>
</rss>
