0

在 RSpec 中,我指定了我期望 Rake 任务具有的行为。任务在位于根目录或项目的 Rakefile 中定义。

rake xml:export据我了解,以下代码在控制台中运行任务。

require 'spec_helper'

RSpec.describe 'xml:export' do
  load 'Rakefile'
  let(:task) { Rake::Task['xml:export'] }

  it '' do
    task.execute
  end
end

但是,该任务需要运行,rake xml:export date=2020-01-01因为它期望ENV['date']传入。

如何在我的规范中提供执行任务的日期?

我已经尝试过system('rake xml:export date=2020-01-01')task.execute但它不起作用。

4

1 回答 1

0

您是否尝试过使用beforeRspec 中的钩子来定义您的日期?(参见:https ://www.rubydoc.info/github/rspec/rspec-core/RSpec%2FCore%2FHooks:before )

您可以将日期定义如下,例如:

      before do
        ENV["date"] = "2020-01-01"
      end

      it '' do
        task.execute
      end

但是,前面的例子不是最推荐的,这里有一个有趣的答案(https://stackoverflow.com/a/27586912/11018979

或者

      before do
        allow(ENV).to receive(:[]).with("date").and_return("2020-01-01")
      end

      it '' do
        task.execute
      end

于 2020-12-23T11:23:32.237 回答