0
votes

In RSpec, I'm specifying the behavior I expect a Rake task to have. The task is defined in the Rakefile located at the root or the project.

From my understanding, the following code runs the task as running rake xml:export in the console.

require 'spec_helper'

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

  it '' do
    task.execute
  end
end

However, The task requires to be run as rake xml:export date=2020-01-01 since it expects the ENV['date'] to be passed in.

How can I provide the date when executing the task in my spec?

I have tried system('rake xml:export date=2020-01-01') instead of task.execute but it doesn't work.

1

1 Answers

0
votes

have you tried to define your date using the hook before in Rspec ? (cf: https://www.rubydoc.info/github/rspec/rspec-core/RSpec%2FCore%2FHooks:before )

You could define your date as following for example :

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

      it '' do
        task.execute
      end

However, the previous example is not the most recommended, here is an interesting answer (https://stackoverflow.com/a/27586912/11018979)

or

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

      it '' do
        task.execute
      end