Tuesday, November 21, 2006

Ruby: Testing Rake Tasks

If you develop in Ruby and you practice Test Driven Development you may consider the question: How do I test Rake tasks?

Let's start with a task that drops each table from a database:
  desc "Wipe the database"
task :wipe => :environment do
connection = ActiveRecord::Base.connection
connection.tables.each do |table|
begin
connection.execute("drop table #{table}")
puts "* dropped table '#{table}'"
rescue
end
end
end
It may be possible to test this task in it's current form; however, rewriting the task can create a familiar and easily testable class. The key is encapsulating the behavior of the task in a class.
  desc "Wipe the database"
task :wipe => :environment do
DatabaseFacade.clean
end

class DatabaseFacade
def self.clean
connection = ActiveRecord::Base.connection
connection.tables.each do |table|
begin
connection.execute("drop table #{table}")
puts "* dropped table '#{table}'"
rescue
end
end
end
end
After moving the behavior into a class method it's possible to test the class the same as any other class.
class DatabaseFacadeTest < Test::Unit::TestCase
def test_clean_drops_all_tables
ActiveRecord::Base.expects(:connection).returns(connection=mock)
connection.expects(:tables).returns(['table1','table2'])
connection.expects(:execute).with("drop table table1")
connection.expects(:execute).with("drop table table2")
DatabaseFacade.clean
end
end

4 comments:

  1. Damn I need to put in some time to learn these new fandangle testing syntaxes, mocks, mocha, etc. That looks sweet.

    ReplyDelete
  2. Thanks for the tip... just started adding tests for my rake tasks, and this was exactly what I was looking for :)

    ReplyDelete
  3. Anonymous4:09 AM

    Where do you put the "testable class"? If I put the testable class in the rake file, how could I require the rake file in my test file? Directly requiring seems not to work...

    ReplyDelete
  4. Thanks for the tip. I decided to shell out to rake instead: http://jjinux.blogspot.com/2009/10/ruby-testing-rake-tasks.html

    ReplyDelete

Note: Only a member of this blog may post a comment.