Ruby-on-rails Rails 4:如何重置测试数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25877734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 4: How to reset test database?
提问by Solomons_Ecclesiastes
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 4 上,注意到我的一些 RSpec 测试失败了,因为我的一些测试重构使用了一个 before 过滤器(大概是因为事务)。这篇文章描述了一个类似的问题:
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:prepareis deprecated in Rails 4. Also, if before transactions like
代替使用 DatabaseCleaner gem,是否有清除测试数据库的 rake 命令?我相信rake db:test:prepare在 Rails 4 中不推荐使用。另外,如果在交易之前
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?
是执着的。是否有另一种重构方法来避免手动清除测试数据库的需要?
回答by ChrisBarthol
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.
你可以在一个 rake 任务中完成这一切并运行它。
Another solution from hereis to include the following your spec_helper.rbfile
这里的另一个解决方案是包含以下spec_helper.rb文件
config.after :all do
ActiveRecord::Base.subclasses.each(&:delete_all)
end
Disclaimer: I have not tested this and you should read the SO postas it may not work in all situations.
免责声明:我尚未对此进行测试,您应该阅读SO 帖子,因为它可能不适用于所有情况。
That being said, I would recommend using the database cleaner gem to avoid situations such as this.
话虽如此,我建议使用数据库清洁器 gem 来避免这种情况。
回答by mpz
It can be:
有可能:
bundle exec rake db:reset RAILS_ENV=test
回答by d1jhoni1b
Sometimes you might need to run this command (optional)
有时您可能需要运行此命令(可选)
rails db:environment:set RAILS_ENV=test
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
rails db:drop db:create db:migrate RAILS_ENV=test
回答by nbirla
You can add an after filter deleting all the entries from the concerned tables.
您可以添加一个后过滤器,删除相关表中的所有条目。
回答by tomr
In theory this ActiveRecord::Migration.maintain_test_schema!should do the trick. Put it in rails_helper.rb
从理论上讲,这ActiveRecord::Migration.maintain_test_schema!应该可以解决问题。把它放进去rails_helper.rb
回答by 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.
我最终编写了一个简单的 rake 任务,根据执行的命令删除/迁移(或删除和迁移)所有测试和开发数据库。
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)).
它包括在发生错误时提示用户是否要继续的功能,并使用 Open3 的 popen3 方法(这样我们可以访问 stdin、stdout 和 stderr;并且任何失败的命令都不会导致 rake 任务的进程中止(与使用system时不同))。
Hopefully this helps someone. :)
希望这有助于某人。:)
https://github.com/xtrasimplicity/rake_all_db_helper/
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.
编辑:但是,只要您想清除数据库,就需要从 shell 手动执行此操作。

