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

Sam Stokes lists at ruby-forum.com
Sun Sep 7 19:58:00 EDT 2008


Hi all,

I'm just getting into RSpec stories and liking them (especially with
webrat), but I'm finding it tricky to write steps that are
self-contained and reusable, particularly where features intersect.
What approaches do people use to achieve this?

(Maybe a better question is, do people bother?  One of the things I like
about story-runner is the way I'm building a DSL for integration testing
my application, but should I just write the scenario I need, write the
steps to make it run and forget about reusing steps?)

As an example of where I'm having trouble, say I'm writing a blog (since
it's the Web 2.0 version of Hello World), so I have posts and comments.
I want to write a scenario something like

----
Given a post
And some comments for the post
When I view the post
Then I should see the comments
----

How do I tell the "Given some comments" step which post to attach the
comments to?

I can do it by having "Given a post" set a @post instance variable and
having "Given some comments" use that, but it feels like global
variables all over again.  My steps (probably in different files) are
coupled via the @post instance variable, and other steps can clobber it,
and if I forget to clear it I might pollute later steps, and if I want
to refer to more than one post (@post1, @post2) I have to rewrite all my
steps...

I noticed a little note on the Cucumber wiki
(http://github.com/aslakhellesoy/cucumber/wikis/home) explicitly
advising against using @variables for this, I'm guessing for these
reasons.

I could avoid the first issue by combining the top two steps into "Given
a post with some comments" but then I still have the problem of which
post "When I view the post" should GET.  Maybe "When I view a post with
comments"...?

Any advice, criticism or sympathy appreciated :)
--
Sam
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list