Sunday
Dec252011

utilities that make unit testing easier

Working with an application that requires many resources and services might take a while to initialize. Such is our Kontera Advertisers Center. When working in development mode it is nice to be able to run our Rspec tests. But although the test themselves run fast (about 300 tests in ~30 seconds), loading the environment takes 40 seconds. To ease the pain of waiting I use a spork!

 

Image taken from http://imgs.xkcd.com/comics/forks_and_spoons.png

 

Spork makes running unit tests faster by shortening the loading time. It requires some modifications to your spec_helper.rb file: (example from AC spec/spec_helper.rb)

require 'spork'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it to take effect.
  ENV["RAILS_ENV"] = 'ci'
  $LOAD_PATH.push File.join(File.dirname(__FILE__),"..")
  require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment'))
  require 'spec/rails'
end
Spork.each_run do
  # This code will be run each time you run your specs.
  Dir[File.expand_path(File.join(File.dirname(__FILE__),'/spec_helpers','**','*.rb'))].each {|f| require f}
end

To use Spork: Install spork gem:

gem install spork

Bootstap spork: (then modify your spec_helper)

spork --bootstrap

Run spork in the background: (from application root)

spork &

Add --drb to the running test, for example:

spec spec/models/permissions.rb -c -fn --drb

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Main | Porting from Ruby 1.8.7 to Ruby 1.9.3: An Oddyssey »