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

Install Spork
Gemfile
  gem 'spork'
create .rspec file
$RAILS_ROOT/.rspec
--drb
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
Gemfile
  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
# 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('config/application.rb')
  watch('config/environment.rb')
  watch(%r{^config/environments/.+.rb$})
  watch(%r{^config/initializers/.+.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb') { :rspec }
  watch('test/test_helper.rb') { :test_unit }
  watch(%r{features/support/}) { :cucumber }
end

Leave a Reply