Skip to content

Instantly share code, notes, and snippets.

@Manfred
Last active August 29, 2015 14:18
Show Gist options
  • Save Manfred/bca184810ece5909d56d to your computer and use it in GitHub Desktop.
Save Manfred/bca184810ece5909d56d to your computer and use it in GitHub Desktop.
Running Rake tasks from your test environment

Running Rake tasks from a spec

This will try to find all the rake tasks defined in the lib/tasks directory and run them. Note that some tasks might need arguments and will fail. I want to keep this example short so dealing with that is left as an exercise for the intern.

require_relative '../spec_helper'
require 'rake'
Dir.glob(Rails.root.join('lib/tasks/**/*.rake')).each do |rakefile|
begin
load rakefile
# There could be an RSpec task which will throw a NameError. Which is a
# good thing, otherwise we would invoke this spec recursively.
rescue NameError => exception
puts '[!] ' + exception.message
end
end
describe "Rake tasks" do
Rake.application.tasks.each do |task|
it "should run `rake #{task.name}'" do
task.invoke
end
end
end
# Load the standard Rails tasks so other tasks can use the :environment
# task. We do this after defining the specs because otherwise we would
# invoke all Rails tasks (like db:drop).
Rails.application.load_tasks
require_relative '../spec_helper'
require 'rake'
Dir.glob(Rails.root.join('lib/tasks/**/*.rake')).each do |rakefile|
begin
load rakefile
rescue NameError => exception
puts '[!] ' + exception.message
end
end
describe "Rake tasks" do
Rake.application.tasks.each do |task|
it "should run `rake #{task.name}'" do
task.invoke
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment