Rspec Integration Test not Cleaning the Database
Asked Answered
F

6

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

Flamingo answered 9/4, 2011 at 21:58 Comment(0)
I
13

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

Illegible answered 9/4, 2011 at 22:0 Comment(0)
H
11

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).

Heat answered 19/10, 2016 at 23:3 Comment(1)
Thanks! Great answer! This is a non-obvious RSpec gotcha for sure.Pharyngoscope
W
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
Wale answered 4/8, 2013 at 3:49 Comment(0)
F
2

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

It describes Database Cleaner besides Rspec and Capybara

Faris answered 9/4, 2011 at 22:3 Comment(2)
Isn't there a way without Database Cleaner?Flamingo
There is, see my answer here: #5916626Wale
W
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.

Whiffle answered 28/6, 2013 at 23:18 Comment(1)
In rspec 3.4, We can check that via group.metadata[:type]Tolmann
H
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
Hidrosis answered 1/6, 2016 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.