6

The database is not being cleaned after each integration test. The value stays in the database.

Is there an option I should have to make this happen?

Thanks

donald
  • 23,587
  • 42
  • 142
  • 223

6 Answers6

13

I think https://github.com/bmabey/database_cleaner is what you need.

mikewilliamson
  • 24,303
  • 17
  • 59
  • 90
10

For anyone using before(:all) hooks, be aware that these hooks are executed before the transaction associated to the fixture is opened. This means that any data created by before(:all) hooks will not be rolled back by transactional fixtures. You can read more in the RSpec documentation.

I just wanted to mention this because I was bit by it and my initial instinct was to jump to Database Cleaner (which wound up not being needed and eventually not working).

TMcManemy
  • 814
  • 9
  • 20
3

How do I prepare test database(s) for Rails rspec tests without running rake spec?

My answer there might be of interest to you. it's a nice solution. For your case, you would probably need something like

config.after :each do
  ActiveRecord::Base.subclasses.each(&:delete_all)
end
Community
  • 1
  • 1
Danyel
  • 2,180
  • 18
  • 32
2

You want DatabaseCleaner, but you may find that the :truncation strategy is a bit too slow to run all the time. It's really only necessary for integration tests, so you can do this:

# spec/spec_helper.rb
require 'database_cleaner'
config.before(:suite) do
  DatabaseCleaner.clean_with :truncation
  DatabaseCleaner.strategy = :transaction
end

config.before(:each) do |group|
  # The strategy needs to be set before we call DatabaseCleaner.start
  case group.example.metadata[:type]
  when :feature
    DatabaseCleaner.strategy = :truncation
  else
    DatabaseCleaner.strategy = :transaction
  end
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

# spec/features/your_feature_spec.rb
require 'spec_helper'
describe "An integration test", :type => :feature do
end

# spec/model/your_model_spec.rb
require 'spec_helper'
describe "A unit test" do
end

Obviously, this only applies if you're doing integration tests with RSpec directly vs. doing them with Cucumber.

Bob Aman
  • 32,839
  • 9
  • 71
  • 95
2

Look here for a tutorial: http://railscasts.com/episodes/257-request-specs-and-capybara

It describes Database Cleaner besides Rspec and Capybara

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • 3
    Isn't there a way without Database Cleaner? – donald Apr 09 '11 at 22:17
  • There is, see my answer here: http://stackoverflow.com/questions/5916126/how-do-i-prepare-test-databases-for-rails-rspec-tests-without-running-rake-spe/18039499#18039499 – Danyel Aug 04 '13 at 03:48
1

There are two ways to accomplish this:

  1. Configure transactional examples for each individual test.
  2. Configure transactional examples for all the tests.

If you opt for option 1: At the top of the spec file, after:

require 'spec_helper'

Add:

RSpec.configure {|c| c.use_transactional_examples = true }

That will roll back the transactions after each example.

2.If you want to configure it globally, then, in the spec_helper.rb

RSpec.configure do |config|
...
config.use_transactional_examples = true # Add this
...
end
Abraham
  • 311
  • 2
  • 8