你如何使用Rake :: TestTask来使用多个任务?

时间:2015-05-01 11:53:55

标签: ruby testing rake minitest rake-task

我和Rake一起运行Minitest,并希望有两个独立的Rake任务。

我添加了以下内容:

require 'rake/testtask'

task :default => [:test]
task :quick => [:unit]

Rake::TestTask.new do |t|
  puts 'within test task'
  t.libs.push 'specs'
  t.pattern = 'specs/*_spec.rb'
  ENV['STACK'] = 'stack1'
  puts "test stack #{ENV['STACK']}"
end


Rake::TestTask.new('unit') do |t|
  puts 'within unit task'
  t.libs.push 'specs'
  t.pattern = 'specs/*_unit.rb'
  ENV['STACK'] = 'stack2'
  puts "test stack #{ENV['STACK']}"
end

当我运行bundle exec rake quick时,我得到了这个输出:

within test task
test stack stack1
within unit task
test stack stack2

我没想到两个任务都要运行。如何创建和运行两个单独的rake任务?就像现在一样,第二个总是覆盖环境变量。

由于

1 个答案:

答案 0 :(得分:2)

您可以使用Rake::Task["task_name"].clear这样解决此问题:

task :test_task do
  Rake::TestTask.new do |t|
    puts 'within test task'
    t.libs.push 'specs'
    t.pattern = 'specs/*_spec.rb'
    ENV['STACK'] = 'stack1'
    puts "test stack #{ENV['STACK']}"
  end
end

task :unit_task do
  Rake::TestTask.new('unit') do |t|
    puts 'within unit task'
    t.libs.push 'specs'
    t.pattern = 'specs/*_unit.rb'
    ENV['STACK'] = 'stack2'
    puts "test stack #{ENV['STACK']}"
  end

end

task :test do
  Rake::Task["unit_task"].clear
  Rake::Task["test_task"].invoke
end

task :unit do
  Rake::Task["test_task"].clear
  Rake::Task["unit_task"].invoke
end