Rails 4: How to Reset Test Database

Rails 4: How to reset test database?

An overkill solution would be:

bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:schema:load RAILS_ENV=test

You could make this all in a rake task and run that.

Another solution from here is to include the following your spec_helper.rb file

config.after :all do
ActiveRecord::Base.subclasses.each(&:delete_all)
end

Disclaimer: I have not tested this and you should read the SO post as it may not work in all situations.

That being said, I would recommend using the database cleaner gem to avoid situations such as this.

Why would rails not reset the test database between runs

We have a more involved FactoryGirl set up that prepares our database with some canonical items, but I think you could probably put this code directly in your test_helper.rb to assure the database is emptied:

# Destroy all models because they do not get destroyed automatically
(ActiveRecord::Base.connection.tables - %w{schema_migrations}).each do |table_name|
ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{table_name};"
end

Alternatively, run rake db:test:prepare before every run.

There is a gem too that you can use, but I don't have any experience with it: http://rubygems.org/gems/database_cleaner.

Reinitialize Rails development and test databases from the current working tree state

You could simplify this to

bundle exec rake db:migrate:reset 
bundle exec rake db:test:prepare

The reset task runs drop, create, migrate. Running migrations dumps the schema anyway (unless you've changed dump_schema_after_migration to false)

The test:prepare step as you know, dumps and loads the schema, and also does a purge (depends slightly on database adapter but basically drops and recreates the database). Given that you have already just dumped the schema, you could change this to

bundle exec rake db:migrate:reset 
bundle exec rake db:test:load_schema

Lastly, the tasks in db:test largely know that they should only run against the test database - regardless of rails env they use the test database when needed, so you could just do

bundle exec rake db:migrate:reset db:test:load_schema 

Lastly, there are some who would also tell you that running migrations from scratch is heresy and that the definitive source of truth is schema.rb (as generated by your production environment)

Why rails 4 tests does not reset db

No, Rails (or RSpec or Minitest) doesn't drop the database before running tests.

Perhaps you used a gem like database_cleaner in the other projects before?

Rails: How to ensure clean state for test database?

You can use database_cleaner gem to ensure a clean state for testing.

You can configure your RSpec like this:

RSpec.configure do |config|

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

Then, you will be able to run your spec only with rspec spec command without resetting the database manually every time.

Clean out, or reset test-database with Rspec and MongoID on Rails 3

If you are using MongoID you can use Database Cleaner with Truncation strategy. E.g.:

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

config.before :each do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
end

config.after do
DatabaseCleaner.clean
end
end


Related Topics



Leave a reply



Submit