Logo: Relish

  1. Sign in

Project: RSpec Rails 2.11


When you run rails generate rspec:install, the spec/spec_helper.rb file
includes the following configuration:

RSpec.configure do |config|
  config.use_transactional_fixtures = true

it means "run every example within a transaction."

the transaction at the end of the example.

Disabling transactions

database_cleaner to do it for you,
simply tell RSpec to tell Rails not to manage transactions:

RSpec.configure do |config|
  config.use_transactional_fixtures = false

Data created in before(:each) are rolled back

Any data you create in a before(:each) hook will be rolled back at the end of
already ran. For example:

describe Widget do
  before(:each) do
    @widget = Widget.create

  it "does something" do
    @widget.should do_something

  it "does something else" do
    @widget.should do_something_else

backing the @widget in each example is new.

Data created in before(:all) are not rolled back

before(:all) hooks are invoked before the transaction is opened. You can use

  1. Be sure to clean up any data in an after(:all) hook:

    before(:all) do
      @widget = Widget.create!
    after(:all) do

    interfere with other examples.

  2. Reload the object in a before(:each) hook.

    before(:all) do
      @widget = Widget.create!
    before(:each) do

    object won't know about those rollbacks so the object and its backing
    data can easily get out of sync.

Last published over 4 years ago by dchelimsky.