Joh*_*ohn 8 testing unit-testing ruby-on-rails oracle11g ruby-on-rails-3
只是想知道是否有办法在不丢弃数据库的情况下运行Rails测试.我目前只执行单元测试,并使用以下rake命令执行此操作:rake test:units.
我在这里先向您的帮助表示感谢!
以防这是相关的:
在 Rails 5(可能还有更早的版本)中,只需注释掉以下行spec/rails_helper.rb:
ActiveRecord::Migration.maintain_test_schema!
Run Code Online (Sandbox Code Playgroud)
这将阻止rake test或rspec尝试删除您的测试数据库。您还需要手动运行迁移。
对于 Rails 5.2,此行为可以在导入之前修改maintain_test_schema设置:falsetest/test_helper.rbrails/test_help
ActiveRecord::Base.maintain_test_schema = false
require "rails/test_help"
Run Code Online (Sandbox Code Playgroud)
rails/test_help将检查 的值maintain_test_schema来决定是否必须删除/创建/迁移测试数据库。
经过一些研究后,我发现没有办法做到这一点。测试 rake 任务将始终删除数据库,即使提供了TEST=Bohdan 建议的选项也是如此。
通过使用该--trace选项,可以证明这一点。这是输出:
$ rake test:units TEST=test/unit/post_test.rb --trace
(in /Users/johnnyicon/Development/ror/test-app)
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:prepare
** Execute test:units
Run Code Online (Sandbox Code Playgroud)
通读Ruby on Rails 测试指南,它描述了其中一些 rake 任务的含义。要特别注意的是任务db:test:load,您可以在输出底部的第 7 行看到该任务,即** Execute db:test:load。指南关于此任务的说明如下:
从当前 schema.rb 重新创建测试数据库
因此,即使我按照 Bohdan 的建议一一执行单元测试,rake 任务仍然会重新创建数据库。这不是我所希望的答案,但这不再是问题了。
我之所以要求开始是因为我无法访问另一个数据库来进行测试,所以我也使用我的开发数据库进行测试。但从那时起,我已经能够获得另一个专用于测试的数据库。
不管怎样,谢谢博丹!我很感激你的帮助!