Let's start with a task that drops each table from a database:
desc "Wipe the database"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.
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
desc "Wipe the database"After moving the behavior into a class method it's possible to test the class the same as any other class.
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
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
Damn I need to put in some time to learn these new fandangle testing syntaxes, mocks, mocha, etc. That looks sweet.
ReplyDeleteThanks for the tip... just started adding tests for my rake tasks, and this was exactly what I was looking for :)
ReplyDeleteWhere 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...
ReplyDeleteThanks for the tip. I decided to shell out to rake instead: http://jjinux.blogspot.com/2009/10/ruby-testing-rake-tasks.html
ReplyDelete