Prevent Rails Test from Deleting Seed Data

Prevent Rails test from deleting seed data

Tests delete all the data from the database and then load your fixtures (if you have any).

You need to get your test helper to load the seed file before the tests run. There are a couple ways to do that, check out my similar question: How to load db:seed data into test database automatically?

The easiest way is probably just to add

require "#{Rails.root}/db/seeds.rb"

to the top of your test_helper.rb file (assuming you use the built-in testing framework).

How to prevent rake test from deleting rake db:seed data?

AFAIK its not usual to use db:seed to load data for your tests, its normally just used to load seed data for development purposes only.

Instead you should create test data in the actual test file. Usually test data is deleted after each test using something like database_cleaner, so each test starts with an empty database.

For example in rspec you can load test data in a let block, before block or in the test itself, e.g

require 'spec_helper'

describe Page do
let(:user) { FactoryGirl.create(:user) }

before do
# create some data
end

it '#name returns XYZ' do
page = FactoryGirl.create(:page, :user => user)
page.description.should == 'XYZ'
end

it '#description returns ABC' do
page = FactoryGirl.create(:page, :user => user)
page.description.should == 'ABC'
end
end

How to stop rspec from dropping the test database before tests

If you don't need to migrate the database you can redefine rspecs-rails
spec:prepare task like this:

lib/tasks/patch_rspec_rails.rb

Rake::Task["spec:prepare"].clear
namespace :spec do
task :prepare do
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
end
end

The original spec:prepare task callstest:prepare, which setups the db.

The task test:prepare exists since Rails 4.0 (or maybe earlier). This task also exists within Rails 5.0. It is a hook for railsties to add test dependent setups. You can check its definition with rake -W test:prepare. That
the task is hit you can check with rake --trace spec.

ActiveRecord uses this task to check the migration state and setup the db.

When this task is not called, no db will be dropped or created.

But be aware, when some other gem uses test:prepare as a hook too plug into tests, it will not work.

Edit:

Since Rails 4.1 you can set config.active_record.maintain_test_schema = false within config/environments/test.rb. This way Rails should no longer try to migrate your test schema.

How to keep data when run test in rails

Very similar to neokain's previous post, however, his didn't work on Rails 3 for me.
I went ahead and dropped this into my Rakefile at the root of the app and when I run test:units, it doesn't blow away all of my existing tables:

Rake::TaskManager.class_eval do
def delete_task(task_name)
@tasks.delete(task_name.to_s)
end
Rake.application.delete_task("db:test:purge")
Rake.application.delete_task("db:test:prepare")
end

namespace :db do
namespace :test do
task :purge do
end
task :prepare do
end
end
end

Clean way to add seeds data in Rails test environment

Here's a very clean answer: How do I load seed.rb within a Rails test environment using rpec?
You use rake RAILS_ENV=test db:seed

How to seed my development database with rails test fixtures

You can trigger the load of your fixtures in your database by using the following rake command:

rake db:fixtures:load

By default, the rails env will be development so the fixtures will load in this environment's database.

How to keep data when run test in rails

Very similar to neokain's previous post, however, his didn't work on Rails 3 for me.
I went ahead and dropped this into my Rakefile at the root of the app and when I run test:units, it doesn't blow away all of my existing tables:

Rake::TaskManager.class_eval do
def delete_task(task_name)
@tasks.delete(task_name.to_s)
end
Rake.application.delete_task("db:test:purge")
Rake.application.delete_task("db:test:prepare")
end

namespace :db do
namespace :test do
task :purge do
end
task :prepare do
end
end
end

getting rake test to seed the database first

So I finally figured this out on my own (spent two days chasing). The db was being seeded from seeds.rb, but I happened to have empty fixture yaml files for the tables I was looking at. Looking at this SO question gave me the idea to delete them, and voila - seed data abounds.

I won't accept this answer for a little while to give someone a chance to provide a better explanation, but thought I'd throw this out there.

Rspec and database_cleaner only remove the data added by the tests

Try to use :truncation for all tests with:

DatabaseCleaner.strategy = :truncation

RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.clean
Rails.application.load_seed
end
end

There also may be an issue with your seeds and not with DatabaseCleaner. You should debug your database state right in the failing test using puts statements or debugger (e.g. pry-byebug).



Related Topics



Leave a reply



Submit