Trying RSpec's Rubyesque Stories
05 Mar 2008Today my pair and I wrote some stories using the rubyesque RSpec story style along with a corresponding step file. It went better then expected. We ended up a story like:
# in stories/expenses/a_user_creating_a_misc_expense_story.rb
Story "User creating misc. expense", %|
As a user
I want be able to submit an expense reimbursement request for a misc. expense
so that I can be reimbursed for business expenditures|,
:steps_for => :expense,
:type => RailsStory do
Scenario "creating a misc. expense unsuccessfully" do
Given "a user at a new expense reimbursement page"
# ....
end
Scenario "creating a misc. expense successfully" do
Given "a user at a new expense reimbursement page"
# ...
end
end
# in stories/steps/expense.rb
steps_for :expense do
Given "a user at a new expense reimbursement page" do
go_to_root
click_new_expense_reimbursement_link
end
# ...
end
One nice thing that came out of this was a small patch to RSpec which David committed tonight involving the options you pass along to Story. Now when declaring a story you can consistently use the *steps_for* option rather then using *steps_for* and/or *steps*.
Previously you could use the *steps* option to pass in a custom StepGroup but that is no longer supported in trunk. Now you use *steps_for* when supplying a custom StepGroup, a symbol or an array of StepGroups and symbols. This helps avoid confusion of passing in *steps* or *steps_for* and not getting what you expect.
Overall there are four story styles (that I'm aware of) in RSpec: plain text stories, rubyesque stories with separate step files, rubyesque stories with inlined steps and rubyesque stories with inlined blocks.
We did try using plain text stories at first, but one drawback of plain text stories is being able to run them individually from within your editor or easily from the command line. We didn't have the need for plain text stories so we opted to not use it.
We started using the rubyesque stories with inlined steps secondly until we got a feel for the stories and then we moved those steps into their own file so we could achieve higher reuse and better step organization.
We knew coming in that we were not going to use the inlined block style since we have used that on another project and it gets clunky really fast.
If you're considering using RSpec stories try the rubyesque stories with step matchers (inlined or in separate files). If you're going to take the plunge into trying plain text stories look to RSpec itself for examples. The actual stories in the stories/ directory helped us get up and running earlier today.