ChatGPT解决这个技术问题 Extra ChatGPT

Rails 4: How to reset test database?

I'm on Rails 4 and have noticed some of my RSpec tests are failing because some of my test refactorings use a before filter (presumably because of transactions). This post describes a similar issue:

rails test database not clearing after some runs

In lieu of using the DatabaseCleaner gem, is there a rake command to clear out the test database? I believe rake db:test:prepare is deprecated in Rails 4. Also, if before transactions like

`post :create, user: Fabricate.attributes_for(:user)`

are persistent, is there an alternative way of refactoring to avoid the need to manually clear out the test database?


C
Community

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.


The first 3 rake db:* may be replaced only with last one as it deletes all tables and creates them from schema.
for Rails 5, use ApplicationRecord instead of ActiveRecord::Base blog.bigbinary.com/2015/12/28/…
Use rake db:structure:load if you're using structure.sql instead of schema.rb.
A neater 1 liner for the above is simply: RAILS_ENV=test bundle exec rake db:drop db:create db:schema:load
p
pjam

It can be:

(For rails Rails 5+)

bundle exec rails db:reset RAILS_ENV=test

For previous versions

bundle exec rake db:reset RAILS_ENV=test

I notice that also runs rake db:seed ...which may or may not be a problem
In case you're using Rails 5, the command is now rails instead of rake: bundle exec rails db:reset RAILS_ENV=test
d
d1jhoni1b

Sometimes you might need to run this command (optional)

rails db:environment:set RAILS_ENV=test

But for sure to wipe out your test database should be as easy as:

rails db:drop db:create db:migrate RAILS_ENV=test


n
nbirla

You can add an after filter deleting all the entries from the concerned tables.


t
tomr

In theory this ActiveRecord::Migration.maintain_test_schema! should do the trick. Put it in rails_helper.rb


X
XtraSimplicity

I ended up writing a simple rake task that drops/migrates (or drops & migrates) all test and development databases, depending on the command executed.

It includes functionality to prompt the user as to whether they would like to continue when an error occurs, and uses Open3's popen3 method (such that we can access stdin, stdout and stderr; and any failed commands don't result in the rake task's process aborting (unlike when using system)).

Hopefully this helps someone. :)

https://github.com/xtrasimplicity/rake_all_db_helper/

edit: This will need to be manually executed from your shell, whenever you would like to clear your database, however.


s
stevec

If you don't want to seed the test database (undesirable in my case), try this:

bundle exec rails db:drop db:create db:migrate RAILS_ENV=test

It will 1. drop, 2. create, and 3. migrate (but not seed) the test database.