Help us learn about your current experience with the documentation. Take the survey.

测试 Rake 任务

为了更轻松地测试 Rake 任务:

  • 使用 RSpec 的元数据标签 type: :task,或者
  • 将你的测试文件(spec)放在 spec/tasksee/spec/tasks 目录下

这样做,RakeHelpers 就会被引入,它提供了一个 run_rake_task(<task>) 方法,让你可以执行 Rake 任务。

所有可用的方法请参见 spec/support/helpers/rake_helpers.rb

通过添加 :silence_stdout 可以重定向 $stdout

示例:

require 'spec_helper'

describe 'gitlab:shell 的 Rake 任务', :silence_stdout do
  before do
    Rake.application.rake_require 'tasks/gitlab/shell'

    stub_warn_user_is_not_gitlab
  end

 describe 'install 任务' do
    it '会调用 create_hooks 任务' do
      expect(Rake::Task['gitlab:shell:create_hooks']).to receive(:invoke)

      run_rake_task('gitlab:shell:install')
    end
  end
end

返回测试文档