rspec/rspec-rails — Issue #492: config.before blocks executed outside of transaction
Author: exviva
Created: 2012-01-23 15:28:07+00:00
State: open

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:

```ruby
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 in not isolated between them:

```ruby
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.