Invoking the same rake task twice in RSpec
Asked Answered
C

2

29

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
Commercialism answered 25/3, 2014 at 15:31 Comment(0)
A
44

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

Awhirl answered 25/3, 2014 at 15:34 Comment(2)
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://mcmap.net/q/502037/-why-is-my-rake-task-running-twice-in-my-testHydatid
I did invoke at setup and reenable at teardownChafer
A
4

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


  after(:each) do
    Rake::Task["task:name"].reenable
  end
Armendariz answered 19/2, 2021 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.