I am developing a ROR app that relies on many custom Rake tasks.
What is the best way to test them?
I like the PivotalLabs' way the most. However, I generalized and modified their code as followed in my app:
# file: spec/tasks/my_rake_file_spec.rb
require 'spec_helper'
require 'rake'
describe 'my_rake_file.rake' do
before :all do
Rake.application.rake_require 'tasks/my_rake_file'
Rake::Task.define_task(:environment)
end
let(:run_rake_task) {
Rake::Task[task_name].reenable
Rake.application.invoke_task task_name
}
describe 'my_task_name' do
let(:task_name) { "my_task_name" }
it "creates 10 cars" do
run_rake_task
Car.count.should == 10
end
end
end
Also, I extracted the heavy lifting of my_rake_file.rake's code to a module, which is stored in lib/