Posted by Elliott Golden
I am working on a Rails 3 CMS Gem that relies on a few Rake tasks. The *.rake files are located within my_gem/lib/tasks. When the Gem is built and installed, the tasks are available as expected from the requiring app. However, calling them from my Cucumber steps results in the Don't know how to build task * message.
Due to my Gem's requirements, I'm building it within vendor of a full Rails app. Running rake -T from this dev environment shows all tasks available except the ones I added to my Gem's lib/tasks directory.
At first, I tried adding a Rails::Railtie#rake_tasks call from Cucumber's env.rb. That is how the tasks are added from the Gem's lib init file for production. However, I could not get the same thing to work in my test env.
A quick look at Rake's options provided the answer.
rake -h
...
-R, --rakelibdir RAKELIBDIR Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')
...
Knowing how to add dirs to Rake's lookup process solved the problem. Running
rake -T -R lib/tasks now showed that my Gem's tasks were now accessible to Rake.
The final piece of the puzzle was to make a simple step definition that I could pass tasks to for execution.
When /^I call the task "([^"]*)"$/ do |task|
`#{task} -R lib/tasks`
end