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

Jarkko Laine jarkko at jlaine.net
Wed Sep 10 01:35:37 EDT 2008


On 10.9.2008, at 0.29, Zach Dennis wrote:
> 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.

I've been bitten by this a couple of times. A common idiom in the  
Rails world is (was at least) to use save! to raise an exception when  
an object couldn't be saved. In a rescue block you could then do the  
things you wanted to do when saving failed. Raising the exception also  
rolled back a transaction, so it was a nice way to ensure some  
transactional logic:

MyModel.transaction do
   MyModel.save!
   OtherModel.save!
end

If either line in the transaction failed, it was ensured that neither  
of them was persisted in the db. This helped in many cases where you  
wanted either all or none of your updates to get into the db. However,  
with stories these cases always fail, because of the reason Zach  
mentions.

The question whether a case where someone posts an empty email address  
is an exceptional state in strict PragProg sense might be a bit  
questionable, of course, but it's beyond the point here...

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




More information about the rspec-users mailing list