Skip to content


Smarticus RSpec Stories On Rails

<p>There has been some chatter on the rspec list as to how to organize your rspec story directory.  By default, you are given a blank slate, and not much direction.  Until the official decree comes down from the <em>makers</em>, I&#8217;m trudging ahead with something that works for me, and will hopefully spark more conversation on how things could be.  If you see any obvious errors or anything I missed, let me know.</p>


<h2>The directories</h2>

stories/ # top level to contain all of our story related stuff
  stories/helper.rb # top level helper
  stories/helpers/ # other helpers like custom matchers and other libs
  stories/steps/ # steps go here
  stories/features/ # top level for features
  stories/features/feature1/ # stories for a feature (features are named after steps)
  stories/features/feature2/ # stories for another feature

The operation

<p>I put together a little rake task that will run your stories if you use the directory structure described above.  Notice, I monkeypatched Spec#run because I couldn&#8217;t figure out why it was barfing.  Thanks <a href="http://pastie.caboo.se/131755">njero</a> for pointing me to that quick fix.</p>

lib/tasks/story.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

require File.join(RAILS_ROOT, "stories", "helper")

module Spec
  class << self; def run; false; end; end
end

namespace :spec do 
  desc "run all rspec stories"  
  task :stories do
    dir = File.join(RAILS_ROOT, "stories", "steps")
    Dir.entries(dir).find_all{|f| f =~ /\.rb\Z/}.each do |file|
      step = file.match(/(\w+)\.rb\Z/).captures[0]      
      require File.join(dir, step)
      with_steps_for step.to_sym do 
        feature_dir = File.join(RAILS_ROOT, "stories", "features", step)
        Dir.entries(feature_dir).reject{|f| f =~ /\A\./}.each do |story|
          run File.join(feature_dir, story), :type => RailsStory
        end
      end
    end    
  end
end
<h2>Other changes</h2>


<p>To complete the example, I&#8217;m including my helper.rb</p>

stories/helper.rb

1
2
3
4
5
6
7
8

ENV["RAILS_ENV"] = "test"
require File.join(RAILS_ROOT, "config", "environment")
require 'spec/rails/story_adapter'

Dir[File.dirname(__FILE__) + "/helpers/**/*.rb"].each do |file|
  require file[0...-3]
end

Posted in Uncategorized.