Using Spring with RSpec and Guard to speed up testing

In my last post I showed you how to setup Guard and RSpec so you can automatically run tests when things change. Now lets get things cooking on gas by using the Spring application preloader. This will mean that your app framework will only have to load once, and tests will be super zippy. Setting up Spring with RSpec is simple and has a huge effect on test running speeds.

Things are zippier on Spring(s)


Getting the sample code for Spring with RSpec

As with all of the examples, I’ve setup the simplest possible example to show setting up Spring with RSpec. Go ahead and checkout the code here:


git clone -b rspec_guard_spring --single-branch https://github.com/eyefodder/spex.git

Once you have checked out the code, jump into the ops directory and vagrant upto bring the VM up. When it’s up and running, get synching running by entering vagrant rsync-auto and then in a new terminal window enter the following:

vagrant ssh
cd /app
bundle exec guard -p

And that is basically it! Now when you run your tests, you will be taking advantage of Spring! Read on to see how to get this working…

How we got it working

So to get this working we did a few things. First off, we added the spring commands for rspec with the spring-commands-rspec gem:

  # rspec_guard_spring
  gem 'spring-commands-rspec'

Next, we let spring create the binstub for us (in the example we already did this, so you will see the outputted file at bin/rspec:

pbh$ spring binstub --all
* bin/rake: spring already present
* bin/rspec: generated with spring
* bin/rails: spring already present

Lastly, we need to update the command that Guard uses to run RSpec in the Guardfile:

guard :rspec, cmd: 'bin/rspec' do
  ...
end

The ‘gem pristine…’ warning

When your specs run you will probably see an error that talks about running gem pristine --all to speed up loading:

Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.

I did a little digging on this and found the gems that are causing the issue using the code posted in this answer:

vagrant@spex:/app$ ruby -e 'p Gem::Specification.stubs.reject(&:stubbed?).reject(&:default_gem?).map(&:name)'
["bigdecimal", "io-console", "json", "minitest", "psych", "rake", "rdoc", "test-unit"]

A little more digging told me that these gems are probably system Ruby packages that appear as gems thanks to the rubygems-integration. Long story short, you have to live with with this warning message for now. Although startup performance might be affected, overall performance each time you run RSpec should still be great! Now you have Spring with RSpec running, our next post will show you how to set up notifications with Growl so you don’t have to keep your terminal window visible while you code.

This entry was posted in Agile Software Development, Engineering, Rails, Ruby, Software Craftsmanship, Test Driven Development. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.