运行 RSpec 多次抽取任务 returns 没有?

Running Rake tasks in RSpec multiple times returns nil?

我猜这与 Rake 只读取文件一次而不倒带有关?但是,我不确定。有什么想法吗?

require 'rake'
require 'rails_helper'

describe 'MyRakeTask' do

  before(:all) do
    Rails.application.load_tasks
  end

  it 'does something sweet' do
    Rake::Task["namespace:my_task"].invoke # runs task
  end

  it 'but it doesnt do it again' do
    Rake::Task["namespace:my_task"].invoke # returns nil
  end

end

Rake docs say invoke will only run the task if it's "needed". The following was pulled from another SO answer 可能有助于澄清:

  • Rake::Task["build"].execute 总是执行任务,但不执行它的依赖项

  • Rake::Task["build"].invoke 执行依赖项,但它只执行任务 它尚未被调用

  • Rake::Task["build"].reenable 首先重置任务的 already_invoked 状态,然后允许任务再次执行,依赖和所有。