Plain Text Stories on Rails

October 22nd, 2007

Since my last post on plain text stories, there have already been a few improvements, not the least of which is that it will now work with Rails. Again, this is trunk (rev 2769+) only and experimental.

<p>Here&#8217;s a working example from an app that I&#8217;m working on:</p>

stories/login

Story: registered user logs in
  As a registered user
  I want to have to log in
  So that only other registered users can see my data

Scenario: user logs in and sees welcome page Given a user registered with login: foo and password: test When user logs in with login: foo and password: test Then user should see the welcome page

Scenario: user logs in with wrong password Given a user registered with login: foo and password: test When user logs in with login: foo and password: wrong Then user should see the login form And page should include text: There was an error logging in.

Scenario: user logs in with wrong login name Given a user registered with login: foo and password: test When user logs in with login: wrong and password: test Then user should see the login form And page should include text: There was an error logging in.

[Update: modified to use runner.steps instead of runner.step_matchers]

stories/login.rb

require File.join(File.dirname(FILE), *%w[helper])

run_story :type => RailsStory do |runner| runner.steps << LoginSteps.new runner.steps << NavigationSteps.new runner.load File.expand_path(FILE).gsub(".rb","") end

Here’s what’s new in this example:

<ul>
<li>run_story is added to the main object so you don&#8217;t have to remember that silly path to the PlainTextStoryRunner which will undoutedbly change!</li>
    <li>run_story accepts arguments, including an options hash, which it will pass to the constructor of the PlainTextStoryRunner (in this case, :type =&gt; RailsStory)</li>
    <li>run_story yields the runner, which now supports a load method which you use to tell it where to find the plain text story file.</li>
    <li>run_story &#8230; runs the story</li>
</ul>


<p>Keep your eyes peeled for more updates in the coming days.</p>

7 Responses to “Plain Text Stories on Rails”

  1. Pat Maddox Says:

    David will you adopt me?

  2. Andy Watts Says:

    Thanks again David. Works great!

    <p>Minor gotcha.  For requiring the step libraries, I added the following to my stories/helper.rb</p>
    
    
    <blockquote>
        <p>Dir[File.join(File.dirname(<i><span class="caps">FILE</span></i>), &#8220;steps/*.rb&#8221;)].each do |file|
    

    require file end

    <p>Should <i>&#8216;ruby script/generate rspec&#8217;</i>  do this?</p>
    
  3. David Chelimsky Says:

    Andy: That’s a good suggestion, but it presents a couple of problems.

    <p>For one, it imposes a convention of calling the subdirectory steps (really, they are step_matchers) and assuming that it is flat. I&#8217;m using step_matchers and I&#8217;m already playing w/ directory hierarchies below that.</p>
    
    
    <p>So we definitely wouldn&#8217;t want to do this until a convention emerges definitively.</p>
    
    
    <p>Even then, there&#8217;s another, more substantial problem: because you can create hierarchies of StepMatchers (which I&#8217;ve been doing with some success &#8211; admittedly only for a day, but success none-the-less), load order is meaningful. As soon as AdminStepMatchers extends LoginStepMatchers, we need to ensure that LoginStepMatchers loads first.</p>
    
  4. Pat Maddox Says:

    hrm…in the case of AdminStepMatchers, I would think you should require login_step_matchers.rb (or whatever) inside the file that defines AdminStepMatchers

  5. David Chelimsky Says:

    Pat – good point.

  6. Aslak Hellesøy Says:

    The story continues: In-browser editor for RSpec stories

  7. Hussein Morsy Says:

    Realy awesome work !!!.

    <p>Does the new plain text story funktion in trunk support other languanges than english ?</p>