Rspec do not rollback db changes in before(:all) #2

leomao10 opened this Issue Jun 1, 2011 · 7 comments


None yet
4 participants

leomao10 commented Jun 1, 2011

I just found that any db changes I make in before(:all) will not automatically rollback when tests were finished. It leave a dirty test environment and broke my specs in next round.

Here is the example:

    describe User
      before(:all) do
        @user =
        # do some very time consuming task that need @user 

      it "should have some consequences" do

      it "should have some other consequences" do

And I will found that there is a user in my test db after I run this spec.

I think it should rollback any db changes I made in before(:all) when it finished running all the specs in that describe block.


dchelimsky commented Jun 1, 2011

transactions are not supported in before(:all)

dchelimsky closed this Jun 1, 2011

leomao10 commented Jun 1, 2011

Thanks for the clarification.


dchelimsky commented Jun 1, 2011

Sure - I need to doc this in a more public place - will try to remember to comment here when I do (though if you google "rspec transactions before(:all)" you'll see a lot of references to it).

Any specific reason why there are no TX in before(:all)?

gucki commented Aug 9, 2011

@dchelimsky We just ran into this as well...

I don't really understand why transactions are not supported for :all but for :each?

May I suggest to at least document it right there: :)


dchelimsky commented Aug 9, 2011

Transaction management is handled by Rails, not RSpec, and Rails has no concept of before(:all) to tap into. Additionally, not all RDBMSes handle nested transactions, which would be a requirement for this to work. This would mean additional confusion/complexity/frustration when users switch databases.

If this is really as big a problem for you as you say, the tools to fix it are at your disposal. You just need to tell Rails (via RSpec) not to manage transactions, and then manage them explicitly via database_cleaner, opening transactions in all before(:all) and before(:each) hooks, and closing them in after(:each) and after(:all) hooks. If you care to publish a gem that does this, I'd be glad to reference it in the documentation, but it is not the common case in my view and I don't want to confuse things any further by adding this to rspec-rails directly.

As for where to doc this, rspec-core is the wrong place as there is nothing rails specific about it. It should be somewhere in I'll give it some thought and get it documented.

thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment