26

I am trying to test a rake task with rspec, and for that purpose I need to invoke it twice but it is only being invoked once.

it 'first test' do
    Rake::Task['my_rake_task'].invoke
    # rake task was processed
end

it 'second test' do
    Rake::Task['my_rake_task'].invoke
    # rake task was NOT processed
end
Guy Segev
  • 1,757
  • 16
  • 24
user3460354
  • 263
  • 3
  • 4

2 Answers2

42

if a rake task has already been invoked once it won't run again unless you call:

@rake[@task_name].reenable

or invoke it with

@rake[@task_name].execute

Guy Segev
  • 1,757
  • 16
  • 24
  • I had to use reenable since execute does not accept arguments, unlike invoke. And then had to fix it running more times then requested - see https://stackoverflow.com/a/45530844/3125034 – iheggie Aug 06 '17 at 10:04
  • 1
    I did `invoke` at `setup` and `reenable` at `teardown` – Sathish Jul 17 '20 at 17:37
3

To adding to Guy Segev answer I prefer adding this to your spec file


  after(:each) do
    Rake::Task["task:name"].reenable
  end
coderVishal
  • 8,369
  • 3
  • 35
  • 56