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.

Here’s a working example from an app that I’m working on:

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:

  • run_story is added to the main object so you don’t have to remember that silly path to the PlainTextStoryRunner which will undoutedbly change!
  • run_story accepts arguments, including an options hash, which it will pass to the constructor of the PlainTextStoryRunner (in this case, :type => RailsStory)
  • 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.
  • run_story … runs the story

Keep your eyes peeled for more updates in the coming days.

7 Responses to “Plain Text Stories on Rails”

  1. Pat Maddox
    Pat Maddox Says:

    David will you adopt me?

  2. Andy Watts
    Andy Watts Says:

    Thanks again David. Works great!

    Minor gotcha. For requiring the step libraries, I added the following to my stories/helper.rb

    Dir[File.join(File.dirname(FILE), “steps/*.rb”)].each do |file| require file end

    Should ‘ruby script/generate rspec’ do this?

  3. David Chelimsky
    David Chelimsky Says:

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

    For one, it imposes a convention of calling the subdirectory steps (really, they are step_matchers) and assuming that it is flat. I’m using step_matchers and I’m already playing w/ directory hierarchies below that.

    So we definitely wouldn’t want to do this until a convention emerges definitively.

    Even then, there’s another, more substantial problem: because you can create hierarchies of StepMatchers (which I’ve been doing with some success – 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.

  4. Pat Maddox
    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
    David Chelimsky Says:

    Pat – good point.

  6. Aslak Hellesøy
  7. Hussein Morsy
    Hussein Morsy Says:

    Realy awesome work !!!.

    Does the new plain text story funktion in trunk support other languanges than english ?

Leave a Reply (Textile Enabled)