[rspec-users] Best practices for sharing state between story steps?

Jonathan Linowes jonathan at parkerhill.com
Mon Sep 8 09:58:36 EDT 2008

On Sep 7, 2008, at 7:58 PM, Sam Stokes wrote:

> What approaches do people use to achieve this?

Perhaps I'm bucking what others have advised against, so take it for  
what it's worth.

I make some limited use of global (instance) variables that  
correspond to english language pronouns. I have things like  
@current_user (corresponds to "I"), @current_project (corresponds to  
"the project"), etc. I am careful to be consistent. There's only a  
handful of these, but I find it extremely convenient. Note, these  
also tend correspond to 'states' in my app, which might be in the  
session or part of a nested URL.

With regard to reusable steps, I have some steps that are generic,  
some are app specific, and some are (group of) story/feature specific:

1. The generic ones are very reusable, for example

   When "I click the $link link" do |link|
     clicks_link link.strip_quotes

   Then "the browser should show $a_or_an $tag tag with $contents" do  
|_, tag, contents|
     response.should have_tag(tag, contents.strip_quotes)

2. The app specific ones are intended to be reusable in any story but  
only my app, may do some database initialization (like fixtures),  
logging in, for example:

   Given "a $pname project with $settings"
   Given "I am logged in as a $role member"

Note, $settings is in the format
	Given a Test project with foo: 1, bar: "bar value", and baz: yadda  

creates a default project overridden with the specified settings  
(using to_hash_from_story, http://www.mail-archive.com/rspec- 
users at rubyforge.org/msg05771.html )

3. And the feature-set specific steps are not very reusable at all.  
As it turns out, there's relatively few of these, and they tend to  
reflect aggregates of more granular steps already tested in a  
different story or scenario.

hope this helps,


More information about the rspec-users mailing list