开发者

Remove test::unit

I forgot to create my application with the -T argument and now it's installed with test::unit. How do I remove test::unit and stop it from creating tests for each controller / mod开发者_如何学编程el generated, after I have already created the application? Thank you


In your config/application.rb try

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

In response to comment

Try

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

May not be the "most appropriate" or Rails-ish way, but it works

Update

I was reading the Rails Initialization Guide today and realized that the most likely reason Test::Unit is still being included is this line:

require 'rails/all'

Which could be replaced with:

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

This should take care of the issue. If you're using Rails 3.1.x you'd also include

require "sprockets/railtie"

if you're planning on using the asset pipeline.

Update 2

For Rails 3.2 you should use this:

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

  c.integration_tool :rspec
  c.performance_tool :rspec
end


To remove Test:Unit in Rails 4:

Replace the following line from config/application.rb:

require 'rails/all'

With:

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

You can safely remove the test directory as well.


Build a new application with rails new APP_NAME --skip-test-unit in Rails 4 results in:

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

in config/application.rb

remove or comment out

require 'rails/all'

and then include

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"

Also if you do not wish to generate system test files add the following:

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

Once this is done, you can delete the test directory


On Rails 6 in config/application.rb replace

require 'rails/all'

For:

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"

Add config.generators.system_tests = nil to the Application block like this:

module YourApp
  class Application < Rails::Application
    ...
    ...

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


The cleanest way, as I mention in a similar question and answer is to do this...

For Rails 3, to remove test_unit from your application, you need to remove (or comment out) the test_unit Railtie in config/application.rb:

# require "rails/test_unit/railtie"

If you are curious about what this line of code does, check out the railties/lib/rails/test_unit/railtie.rb source which currently looks like this:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜