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

Closed
leomao10 opened this Issue Jun 1, 2011 · 7 comments

Comments

Projects
None yet
4 participants

leomao10 commented Jun 1, 2011

Hi,
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 = User.new
        # do some very time consuming task that need @user 
      end

      it "should have some consequences" do
      end

      it "should have some other consequences" do
      end
    end

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.

Owner

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.

Owner

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: http://relishapp.com/rspec/rspec-core/v/2-6/dir/hooks/before-and-after-hooks :)

Owner

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 http://relishapp.com/rspec/rspec-rails. 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