Testing with Rails3.1 is quite slow.So I tried Spork to speed it up.
Spork is a kind of server for testing. Using that,we can skip initialization of rails app which is too heavy for each testing.
While Spork is aimed to run with RSpec,I'm using test:unit (it's rails' default,as you know). So I put Spork and Spork-testunit gems in Gemfile.
group :test, :development do
gem 'spork'
gem 'spork-testunit'
end
then,as usual
$ bundle install
after that,we have to prepare test_helper.rb for Spork.First,execute the command like this
$ spork testunit --bootstrap
Now,in test/test_helpr.rb,you can find Spork.prefork and Spork.each_run block.
So I put some code in those block as follows
Spork.prefork do
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
fixtures :all
def login(user_id=nil)
@request.session[:user_id] = user_id
end
end
end
Spork.each_run do
load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
end
Weird thing is that I have to load all the files in each_run block explicitly. However,without that,Spork server couldn't get any changes I've made on models or controllers. It could be bug,no?
Then start Spork server
$ spork
To do actual testing,do like this
$ testdrb -Itest test/units/*rb
Testing will be executed with Spork server.
In my case,it took less than half the time of simple "rake test" testing. Worth trying.