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

Zach Dennis zach.dennis at gmail.com
Tue Sep 9 17:29:04 EDT 2008

On Tue, Sep 9, 2008 at 3:52 PM, Matt Wynne <matt at mattwynne.net> wrote:
> On 9 Sep 2008, at 19:52, Jim Morris wrote:
>> aslak hellesoy wrote:
>>> 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.
>> I think that is the debate, but I'd like to point out that there is always
>> state between steps, it is just a matter of where it is being kept. In your
>> example for instance most of the state is being kept in the database between
>> steps (ie between Given an "active" site_user names "aslak" and he following
>> steps.
>> (As a side question how do you clean up the database between tests, so the
>> "state" from the previous Scenario doesn't affect the next Scenario?) In
>> some cases I use a randomly generated record each time.
> I think the scenarios are each wrapped in a transaction, so (as long as
> you're using the right type of database / tables) the slate should be wiped
> clean between each one.

I don't think this works as you may expect since My
SQL and PostgreSQL don't support nested transactions, which is what
would happen if your test environment wrapped stories/scenarios in
transactions and your application utilized transactions.

Granted there has been hopes of getting savepoints fix the problem,
but the ticket has not been resolved:


Zach Dennis

More information about the rspec-users mailing list