Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Rake tasks in RSpec multiple times returns nil?

I'm guessing this has to do with Rake reading the file once and not rewinding it? But, I'm not sure. Any ideas?

require 'rake'
require 'rails_helper'

describe 'MyRakeTask' do

  before(:all) do
    Rails.application.load_tasks
  end

  it 'does something sweet' do
    Rake::Task["namespace:my_task"].invoke # runs task
  end

  it 'but it doesnt do it again' do
    Rake::Task["namespace:my_task"].invoke # returns nil
  end

end
like image 376
daino3 Avatar asked Sep 03 '15 16:09

daino3


1 Answers

The Rake docs say invoke will only run the task if it's "needed". The following was pulled from another SO answer and might help clarify:

  • Rake::Task["build"].execute always executes the task, but it doesn't execute its dependencies

  • Rake::Task["build"].invoke executes the dependencies, but it only executes the task if it has not already been invoked

  • Rake::Task["build"].reenable first resets the task's already_invoked state, allowing the task to then be executed again, dependencies and all.

like image 92
eeeeeean Avatar answered Oct 21 '22 17:10

eeeeeean