I just went through this myself yesterday, and was considering
posting an article on my blog, but figured it’d be too short lived,
once the doc page is up on the rspec site. So I’ll post here what
I’ve cobbled together from various other articles, emails, and
generous help on #rspec.
- DECIDE A DIRECTORY STRUCTURE AND FILL OUT helper.rb
Directory structure:
stories/
stories/steps/
stories/stories/
stories/stories/foo
stories/stories/foo/a_foo_story
stories/stories/foo/a_foo_story.rb
stories/stories/foo/another_foo_story
stories/stories/foo/another_foo_story.rb
stories/all.rb
stories/helper.rb
File: stories/helper.rb:
ENV[“RAILS_ENV”] = “test”
require File.expand_path(File.dirname(FILE) + “/…/config/
environment”)
require ‘spec/rails/story_adapter’
Dir[‘stories/steps/**/*.rb’].each do |steps_file|
require steps_file
end
add extra include’s and helper methods here
include FixtureReplacement
- WRITE A SHORT TEXT STORY
File: stories/stories/admin/admin_accounts
Story: admin user manages accounts
As an admin user
I want site-wide access to accounts, projects, and users
So I can create and modify items
Scenario: admin user logs in
Given an admin user and account
And a bunch of accounts setup
When user logs in with email: [email protected] and password:
secret
Then browser should show the Admin page
And browser should show the Accounts table
- WRITE A rb SCRIPT FOR THE STORY
File: stories/stories/admin/admin_accounts.rb
require File.join(File.dirname(FILE), “…/…/helper”)
with_steps_for :misc do
run File.expand_path(FILE).gsub(".rb",""), :type => RailsStory
end
For other stories, just copy this file, and add/change the steps
groups as required. The rest of the file is unchanged (it gets the
text story filename from its own name.)
3.A And Run It:
$ ruby stories/stories/admin/admin_accounts.rb
should show 1 scenarios: 0 succeeded, 0 failed, 1 pending
- CODE THE STEPS
To start, I put all my steps into a “miscellaneous” steps file until
patterns emerge when I’ll separate them out into different steps files
File: stores/steps/misc_steps.rb
steps_for(:misc) do
Given “an admin user and account” do
@user = create_user( :email => “[email protected]”, :password =>
‘secret’, :site_admin => true)
end
Given “a bunch of accounts setup” do
4.times do
create_account
end
end
When “user logs in with email: $email and password: $password” do |
email, password|
post_via_redirect “/sessions”, :email => email, :password =>
password, :account => account
response.should be_success
session[:user].should == @user.id # should_be_logged_in(@user)
end
Then “browser should show the $h1_contents page” do |h1_contents|
response.should have_tag(“h1”, h1_contents)
end
Then “browser should show the $table_name table” do |table_name|
response.should have_tag(“div h2”, table_name)
end
end
4.A And Run It
$ ruby stories/stories/admin/admin_accounts.rb
or
$ ruby stories/all.rb
–linoj