I’m new to Ruby, and I’ve been trying to learn Rake, RSpec, and Cucumber. I found some code that will help me test my Rake tasks, but I’m having trouble getting it to work. I was told here: http://blog.codahale.com/2007/12/20/rake-vs-rspec-fight/ to drop this:
def describe_rake_task(task_name, filename, &block)
require "rake"
describe "Rake task #{task_name}" do
attr_reader :task
before(:all) do
@rake = Rake::Application.new
Rake.application = @rake
load filename
@task = Rake::Task[task_name]
end
after(:all) do
Rake.application = nil
end
def invoke!
for action in task.instance_eval { @actions }
instance_eval(&action)
end
end
instance_eval(&block)
end
end
into my spec_helper.rb file.
I’ve managed to take this code out and run it in my cucumber steps like this:
When /^I run the update_installers task$/ do
@rake = Rake::Application.new
Rake.application = @rake
load "lib/tasks/rakefile.rb"
@task = Rake::Task["update_installers"]
for action in @task.instance_eval { @actions }
instance_eval(&action)
end
instance_eval(&block)
Rake.application = nil
end
but when I try to get things working in rspec, I get the following error.
ArgumentError in ‘Rake task
install_grapevine should install to
the mygrapevine directory’wrong number of arguments (1 for 2)
/spec/spec_helper.rb: 21:ininstance_eval'block in invoke!’
/spec/spec_helper.rb: 21:in
/spec/spec_helper.rb: 20:ineach'invoke!’
/spec/spec_helper.rb: 20:in
/spec/tasks/rakefile_spec.rb:12:in `block (2 levels) in
‘
Unfortunately, I’ve got just under a week of ruby under by belt, so the metaprogramming stuff is over my head. Could anyone point me in the right direction?
This works for me: (Rails3/ Ruby 1.9.2)
Substitute your rake task name here and also note that your require may be “lib/tasks/cron” if you don’t have the lib folder in your load path.
I agree that you should only do minimal work in the Rake task and push the rest to models for ease of testing. That being said I think it’s important to ensure that the code is ACTUALLY run in my cron tasks during my integration tests so I think very mild testing of the rake tasks is justified.