[rspec-users] Reusing story snippets

David Chelimsky dchelimsky at gmail.com
Sat Jun 14 10:40:32 EDT 2008

On Jun 14, 2008, at 5:03 AM, Mikel Lindsaar wrote:

> I find myself doing this:
> Scenario "logged in user visiting the home page" do
>  Given "A logged in user" do
>    a_logged_in_user
>  end
>  When "..."
>  Then "..."
> end

Things have evolved a bit since Story Runner first came out. The  
approach you are using here is what we call in-line steps, and was the  
only option back then.

You can now use disconnected steps in both plain text and Ruby:

require 'steps/visitors'

Story "logged in users see more stuff", %(
   As a registered user
   I want to see more stuff than non-registered users
   So I can feel like I'm getting some benefit in return for giving
     up my personal information
), :steps => :visitors do
   Scenario "logged in user visits home page" do
     Given "I am logged in as David"
     When "I visit the home page"
     Then "I should see the message 'Welcome David'"

# in steps/visitors

steps_for :visitors do
   Given "I am logged in as $name" do |name|
     # create a user w/ name and log in as that user

   When ".."
   Then ".."

This approach really cleans up the story code leaving the informative  
bits while hiding the redundant detail.


> The a_logged_in_user method is a helper method in helper.rb which sets
> up the state so that the user can browse the website.
> Later in the story of course, I can just do 'Given "A logged in user"
> and it will get the previous definition.
> Is there any way to avoid that duplicated Given call at the top of
> almost every story?
> Mikel
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list