namespace :test do desc "Heckle a class using Test::Unit" task :heckle => :environment do heckle(:test) end end namespace :spec do desc "Heckle a class using rspec" task :heckle => :environment do heckle end end private def heckle(target = :spec) unless ENV.include?("model") raise "usage: rake #{target.to_s}:heckle model=User" end #The user is allowed to test a single method #with User#activate! or similar inputs = ENV["model"].split("#") model = inputs[0].capitalize test_prefix = "#{model.downcase}" klass = Object.const_get("#{model}") #First check to see if user provided #an instance method name. ex: User#activate! methods = [inputs[1]] if inputs.size == 2 #If the user only passed in a model name, #we need to get the instance methods that #are not part of ActiveRecord::Base methods ||= klass.instance_methods - ActiveRecord::Base.instance_methods methods.each do |method| cmd = "heckle #{model} #{method} -t test/unit/#{test_prefix}_test.rb" if target == :test cmd ||= "spec spec -H #{model}##{method}" if target == :spec system(cmd) end end