轨道上的红宝石 - 删除测试::单位



我忘了使用 -T 参数创建我的应用程序,现在它安装了 test::unit。在我创建应用程序后,如何删除 test::unit 并阻止它为生成的每个控制器/模型创建测试?谢谢

在你的 config/application.rb 中尝试

config.generators do |g|
  g.test_framework :rspec #=> or whatever
end

回应评论

尝试

config.generators do |g|
  g.test_framework nil
end

可能不是"最合适"或类似 Rails 的方式,但它有效

更新

我今天正在阅读 Rails 初始化指南,并意识到仍然包含 Test::Unit 的最可能原因是这一行:

require 'rails/all'

可以替换为:

require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"

这应该可以解决问题。如果您使用的是 Rails 3.1.x,您还将包括

require "sprockets/railtie"

如果您计划使用资产管道。

更新 2

对于 Rails 3.2,你应该使用这个:

config.app_generators do |c|
  c.test_framework :rspec, :fixture => true,
                           :fixture_replacement => nil
  c.integration_tool :rspec
  c.performance_tool :rspec
end

要删除 Rails 4 中的 Test:Unit:

替换 config/application.rb 中的以下行:

require 'rails/all'

跟:

require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"

您也可以安全地删除测试目录。

Rails 4 中使用 rails new APP_NAME --skip-test-unit 构建一个新应用程序,结果是:

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

On Rails 5.1 and Rails 5.2

config/application.rb

删除或注释掉

require 'rails/all'

然后包括

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

此外,如果您不希望生成系统测试文件,请添加以下内容:

# Don't generate system test files.
config.generators.system_tests = nil

完成此操作后,您可以删除test目录

config/application.rbRails 6替换

require 'rails/all'

为:

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

config.generators.system_tests = nil添加到应用程序块,如下所示:

module YourApp
  class Application < Rails::Application
    ...
    ...
    # Don't generate system test files.
    config.generators.system_tests = nil
  end
end

正如我在类似的问题和答案中提到的,最干净的方法是这样做......

对于 Rails 3,要从应用程序中删除test_unit,您需要删除(或注释掉)test_unit Railtie config/application.rb

# require "rails/test_unit/railtie"

如果你对这行代码的作用感到好奇,请查看railties/lib/rails/test_unit/railtie.rb源代码,它目前看起来像这样:

module Rails
  class TestUnitRailtie < Rails::Railtie
    config.app_generators do |c|
      c.test_framework :test_unit, :fixture => true,
                                   :fixture_replacement => nil
      c.integration_tool :test_unit
      c.performance_tool :test_unit
    end
    rake_tasks do
      load "rails/test_unit/testing.rake"
    end
  end
end

相关内容

  • 没有找到相关文章

最新更新