The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby: Testing Rake Tasks

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jay Fields

Posts: 765
Nickname: jayfields
Registered: Sep, 2006

Jay Fields is a software developer for ThoughtWorks
Ruby: Testing Rake Tasks Posted: Nov 21, 2006 6:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jay Fields.
Original Post: Ruby: Testing Rake Tasks
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jay Fields
Latest Posts From Jay Fields Thoughts

Advertisement
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

Read: Ruby: Testing Rake Tasks

Topic: The Best Ruby IDE For Windows Previous Topic   Next Topic Topic: Ruby IDE - Screenshots of Ruby In Steel

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use