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

Rick DeNatale rick.denatale at gmail.com
Wed Sep 10 08:24:36 EDT 2008

On Tue, Sep 9, 2008 at 8:42 AM, aslak hellesoy <aslak.hellesoy at gmail.com>wrote:

> On Tue, Sep 9, 2008 at 2:21 PM, Dan North <tastapod at gmail.com> wrote:
> > Hi Jim.
> >
> > I guess I'm not a purist then - that looks fine to me, and it's probably
> > something I would consider doing too.

> The debate seems to be whether step definitions should be stateful or not.
> In practice this is achieved by setting one or more @variables in a
> step and reusing them in a different step - all within a scenario.
> Both are fine, but beware that as your codebase grows and you have
> hundred or so step definitions, things can become *really* hard to
> maintain.
> It won't bite you when you start on a new codebase, but in my
> experience stateful steps will bite you later.
> Having experienced this pain in one project, I decided to try out
> stateless steps exclusively on the next project. I've found stateless
> steps to be a tad more verbose than stateful ones, both in the text
> and in the implementation (because you have to pass "identifiers"
> around). Still, the improved maintainability of my features and
> scenarios using this approach outweighs this slight increase in
> verbosity.
> Here is an example of a stateless scenario: (As a convention I'm
> "quoting" variables)
> http://github.com/aslakhellesoy/ba/tree/master/features/submit_proposal.feature
> You can see the extra verbosity I'm talking about in the repetition of
> "Aslak", "Beerfest" etc.
> I'm still pragmatic about this of course, but now you know some of my
> experience.

I've done something similar, but I might have handled the steps
implementation a bit differently in that I'm not sure my approach would be
called stateless.

For example say I had (in a very abstract form)

    Given 'Rick' has admin privileges
    Then 'Rick' can do admin function X

In the  step for the given, I'd get or create an admin user named rick, and
set an instance variable say @admin
Then in the step for the then, I'd have an expectation that the name of
@admin matched the name given in the step.
This was a way of catching cases where the story mixed up actors.

Although the short example doesn't really emphasize the problem I was trying
to solve, I'm not sure in retrospect whether or not this was a valuable
thing to do.

Rick DeNatale

My blog on Ruby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080910/eae3a2a8/attachment-0001.html>

More information about the rspec-users mailing list