Testing your rake tasks
This week I’ve been working on a client project where I needed to import a massive XML dataset into a database. The XML was non-standard and broke its own rules in several places. Consequently my rake task quickly become very complicated and I needed some tests to ensure I wasn’t breaking previous work. This leads to a rather interesting question: how do you test rake tasks? After a bit of googling I found a rather neat solution of simply pulling out the rake code to a class. For example, rather than:
desc "Import the XML"
task :import => :cleanup do
File.open(XML_FILE) do |file|
# Do importing here...
end
end
do something more like
desc "Import the XML"
task :import => :cleanup do
File.open(XML_FILE) do |file|
XMLImporter.parse_lines(file)
end
end
XMLImporter can then be tested in the normal way. Good ruby - clean and effective.
No comments yet. Be the first.
Leave a reply








