config.before blocks executed outside of transaction #492

Closed
exviva opened this Issue Jan 23, 2012 · 2 comments

Comments

Projects
None yet
3 participants

exviva commented Jan 23, 2012

I'm not sure whether this is a bug or intended (but undocumented) behaviour, but blocks defined with config.before are executed outside of Active Record's transactions.

I created a sample Rails app that reproduces this problem: https://github.com/exviva/rspec_before_transactions.

Notice the spec_helper.rb:

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.before do
    Post.create!
  end
end

Then, there are two examples in post_spec.rb, which expose the fact that database state is not isolated between them:

describe Post do
  it 'executes config.before within the transaction' do
    Post.count.should eq(1)
    Post.create!
    Post.count.should eq(2)
  end

  it 'executes config.before within the transaction the second time' do
    Post.count.should eq(1)
    Post.create!
    Post.count.should eq(2)
  end
end

I managed to reproduce it using RSpec 2.8 and Rails 3.1.3 and 3.2.0. I haven't tried any other version.

Owner

dchelimsky commented Jan 23, 2012

Pretty sure this has the same root cause as #391.

Contributor

alindeman commented Jun 2, 2012

#391 is resolved; going to resolve this one too. If that's not appropriate, let me know and we can reopen.

@alindeman alindeman closed this Jun 2, 2012

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