Rake :: TestTask Rails 5 Uninitialize Constant

时间:2017-11-29 18:56:17

标签: ruby-on-rails

我试图将我的测试分解为多个测试任务:

Rails.configuration.use_database_cleaner = true

  namespace :test do
    Rails::TestTask.new(api: "test:prepare") do |t|
    t.pattern = 'test/api/**/*_test.rb'
  end

  task :jasmine_ci_in_another_process do
    if !system("./bin/rake jasmine:ci") 
      fail
    end
  end

  task :capybara_in_another_process do
    if !system("./bin/rake test:capybara")
      fail
    end
  end

  Rails::TestTask.new(capybara: [:prepare]) do |t|
    t.pattern = 'test/**/capybara/*_test.rb'
  end

  Rails::TestTask.new(unit: [:prepare]) do |t|
    t.test_files = FileList['test/**/*_test.rb'].exclude(
      'test/firebase/**/*_test.rb',
      'test/**/capybara/*_test.rb'
    )
  end

  task :ci => [:prepare, :jasmine_ci_in_another_process, :unit, :capybara_in_another_process]
 end

if !ENV['SKIP_JASMINE'] && ENV['TEST'].blank?
  Rake::Task[:test].enhance ['test:api', 'test:jasmine_ci_in_another_process']
end

每当我去运行rake任务时,无论是rake test还是rake test:unit等,我都会收到错误,说明以下内容: “NameError:未初始化的常量Rails :: TestTask”。如果我只有一个Rails::TestTask.new的声明似乎一切都很好,那么我的测试就会证明我希望它们是如何。

2 个答案:

答案 0 :(得分:0)

你想用Rails :: TestUnit :: Runner替换Rails :: TestTask。

我认为你的这个测试,例如:

Rails::TestTask.new(capybara: [:prepare]) do |t|
    t.pattern = 'test/**/capybara/*_test.rb'
end

将在Rails 5.1中运行,如下所示:

require "rails/test_unit/runner"

namespace :test do
  task :capybara => "test:prepare" do
    $: << "test"
    test_files = FileList['test/**/capybara/*_test.rb']
    Rails::TestUnit::Runner.run(test_files)
  end
end

在Rails 5.1.6中测试

应该在2015年5月发布jonatack:https://github.com/rails/rails/issues/19997

答案 1 :(得分:0)

只需在Anatortoise House的Rails 6中添加答案即可。 这还包括排除文件。

# lib/tasks/test.rake
Rake::Task["test:system"].clear

namespace :test do
  desc "Run all system tests except test/serial folder"
  task system: "test:prepare" do
    $: << "test"
    test_files = FileList["test/system/**/*_test.rb"].exclude(
      "test/system/serial/**/*_test.rb"
    )
    Rails::TestUnit::Runner.run(test_files)
  end

  desc "Run all serial test folder"
  task serial: "test:prepare" do
    $: << "test"
    test_files = FileList["test/system/serial/*_test.rb"]
    Rails::TestUnit::Runner.run(test_files)
  end
end