Cjmarkham
Cjmarkham

Reputation: 9681

Rspec testing rake tasks

I have a simple rake task with a puts in it

namespace :atask
  task something: :environment do
    puts 'Foo'
  end
end

My spec is as follows

describe 'atask:something' do
  before { MyApp.Application.load_tasks }

  context 'one' do
    it do
      Rake::Task['atask:something'].invoke
    end
  end

  context 'two' do
    it do
      Rake::Task['atask:something'].invoke
    end
  end

  context 'three' do
    it do
      Rake::Task['atask:something'].invoke
    end
  end
end

As you can see it has 3 contexts and 3 it blocks. So I should see 'Foo' in the console three times, once for each time the task is invoked. However, I only see 'Foo' once in the console even though it is invoked three times.

Upvotes: 5

Views: 826

Answers (1)

Cjmarkham
Cjmarkham

Reputation: 9681

It turns out that when a rake task has been invoked it won't be ran again. You have to either reenable the task after invocation or use execute

Upvotes: 5

Related Questions