Speedup Testing with Spork

Speeding up Testing

Testing a rails app can sometimes really suck… Part of it is loading the rails stack (20-30s), and part of it is how you design some aspects around test data creation (if it is expensive) and user login, etc.

A good combination to help on the technical side of things is spork and guard, as outlined here.

Once you have installed the necessary, you can use it quite simply:

<rails root>bundle exec guard start


Install Spork
  gem 'spork'
create .rspec file
initial config
bundle exec spork --bootstrap
trial run
v2$ bundle exec spork
    Using RSpec, Rails
    Preloading Rails environment
    Loading Spork.prefork block...
    Spork is ready and listening on 8989!
Get it to work You have to use whatever code you currently have in your startup/spec_helper stuff for cucumber or rspec. And maybe add some more code when things still aren’t quite right! Or maybe remove some… Basically, move all of your spec_helper code into the prefork block, and adjust as necessary to make errors go away.
install Guard
  gem 'rb-fsevent'
  gem 'guard-spork'
initial config
guard init spork
trial run
bundle exec guard start
Get it to work You might need to add some tweaks to the Guardfile. For example, increasing the wait time, and maybe adding other files to watch over for changes.

# Guardfile Configuration
# More info at https://github.com/guard/guard#readme
guard 'spork', :wait => 50, :cucumber_env => { 'RAILS_ENV' => 'test' },
               :rspec_env => { 'RAILS_ENV' => 'test' } do
  watch('spec/spec_helper.rb') { :rspec }
  watch('test/test_helper.rb') { :test_unit }
  watch(%r{features/support/}) { :cucumber }

Leave a Reply