I am developing a ROR app that relies on many custom Rake tasks.
What is the best way to test them?
Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.
You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks . Each task has a description, and should help you find the thing you need.
Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.
You can use abort("message") to gracefully fail rake task. It will print message to stdout and exit with code 1.
Rake tasks are pretty hard to test. The easiest solution would be to move the logic into a method in an appropriate model. You can then test that and just call the method from the rake task.
Something like:
def execute_rake(file,task) require 'rake' rake = Rake::Application.new Rake.application = rake Rake::Task.define_task(:environment) load "#{Rails.root}/lib/tasks/#{file}" rake[task].invoke end
At your spec:
execute_rake("tags.rake","tags:popular")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With