how to clean database before running the each spec file? - ruby-on-rails

I want to clear my test database before running the each spec files.
I am already using rspec with factory girl.
Thanks,
Hare

Add to RSpec.configure block in your spec_helper.rb
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
Must work

In your spec_helper.rb,
inside the RSpec.configure block
RSpec.configure do |config|
# ...
config.before(:suite) do
DatabaseCleaner.clean_with :transaction
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:all) do
DatabaseCleaner.start
end
config.after(:all) do
DatabaseCleaner.clean
end
# ...
end
before(:all) and after(:all) runs for every spec file and not before and after the whole suite. So, for every spec file you will be able to clear database using any of the three strategies :transaction, :truncation, :deletion

This is what I typically do for DatabaseCleaner
# Database Cleaner
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
That will make sure you have a clean database for each test.
Checkout a related, albeit old, article by Avdi for more info.

Related

Resetting table IDs and re-creating the test DB with Rspec on Rails

I have various tests that create models using FactoryGirl in Rails
it "does something" do
user = create(:user)
...
expect(user.name).to eq("foo")
end
I noticed that each time I run the spec suite, it does not reset the table id's on the test schema (using Postgres). So after several executions the user might have an id of something like 2894.
I use Database Cleaner to clean my DB between subsequent runs - see below.
Is there a way to reset table ID's directly between rspec runs? A company I previously worked at did it with their app, but I'm not aware how.
If #1 is not an option, is it possible to drop and recreate the test DB between runs?
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:deletion)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :deletion
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
I am also using DatabaseCleaner gem and don't experience the same issue you do (though I'm using mysql instead).
Not sure if this helps in your case but would be worth trying my configuration:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
Rails.application.load_seed
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
My rails_helper.rb
require 'support/database_cleaner'
RSpec.configure do |config|
config.use_transactional_fixtures = false

DB Cleaner not cleaning database after each test after switching from sqlite to PostgreSQL

This config worked fine when I was using sqlite:
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
controller.stub(:should_navigate_user?).and_return(false) rescue ""
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
When I switched to PostgreSQL I began getting errors when tests depended on the id number of entries in the database. I looked at the database during the tests and it was never cleaning the database.
If I switch to the following config:
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.clean
controller.stub(:should_navigate_user?).and_return(false) rescue ""
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
All of my tests pass, but I'm having to call clean before and after my methods tests?
I shouldn't have to clean the database before and after each test - right? I feel like I'm misunderstanding something somewhere. What am I failing to understand.
I'm using postgres with the database cleaner. Its working well now, but I remember having trouble at first. I pasted my config below. You can stick this in your spec_helper.rb, or in another file such as /support/database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Also make sure you turn the transactional fixtures off in the spec_helper:
config.use_transactional_fixtures = false
You used sqlite in non-persistent mode. PostgreSQL doesn't have such options, always persistent. Use drop database/create database to clean it complettely.

Rails Rspec Capybara and DatabaseCleaner - using truncation only on feature tests

I saw this cool method for only using Database cleaners :truncation for capybara test using :js => true
In spec_helper.rb:
config.before(:each) do
DatabaseCleaner.strategy = if example.metadata[:js]
:truncation
else
:transaction
end
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
The problem is that any feature test done with capybara seems to need the cleaning strategy to be :truncation.
All the other specs, however, are fine with :transaction, which is significantly faster.
Is there a way of specifying strategy for only capybara feature tests? SOmething like:
DataCleaner.strategy( :truncation ) if :type => :feature
this should do it, let me know
config.after(:all, :type => :feature) do
DatabaseCleaner.clean_with :truncation
end

SQLite3::SQLException when using database_cleaner with Rails / Spork / RSpec

When attempting to follow example on database_cleaner's GitHub page, I encountered the following error from RSpec:
ActiveRecord::StatementInvalid:
SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction
The configuration used in spec_helper.rb is:
require 'spork'
require 'database_cleaner'
Spork.prefork do
# .. snip
RSpec.configure do |config|
# .. snip
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
end
The accepted answer makes all tests slower (when it's not needed) by truncating after each one.
Just add
config.use_transactional_fixtures = false
when using database_cleaner.
If you have both config.use_transactional_fixtures = true and DatabaseCleaner.strategy = :transaction you're going to start a transaction inside another transaction and that's not allowed.
I found the solution to be changing the entire strategy to :truncation. Updated spec_helper:
require 'spork'
require 'database_cleaner'
Spork.prefork do
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
end

Speed Up tests without database interaction

I've added some :js => true tests to my project and had to integrate the database_cleaner gem to make it work. Now my tests, even the one's that don't use a database are slowed down considerably. Is there a way to skip database access for mocked/non-db-based tests?
Relevant part of the spec_helper.rb
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
You can use DatabaseCleaner only if it need
spec_helper.rb
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
if :without_db != example.metadata[:type]
DatabaseCleaner.start
end
end
config.after(:each) do
if :without_db != example.metadata[:type]
DatabaseCleaner.clean
end
end
user_spec.rb
describe User, :type => :without_db do
it 'should be valid' do
should be_valid
end
end

Resources