Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Rake Tasks

Tags:

I am developing a ROR app that relies on many custom Rake tasks.

What is the best way to test them?

like image 732
collimarco Avatar asked Aug 10 '09 14:08

collimarco


People also ask

What are rake tasks?

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.

How do I see list of rake tasks?

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.

How do you run a rake test?

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.

How do you fail a rake task?

You can use abort("message") to gracefully fail rake task. It will print message to stdout and exit with code 1.


2 Answers

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.

like image 123
Olly Avatar answered Oct 13 '22 08:10

Olly


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") 
like image 42
Tiago Albineli Motta Avatar answered Oct 13 '22 08:10

Tiago Albineli Motta