How to speed up your rspec workflow

Unit testing with rails might get slow, but there are tools that improve the speed and the comfort of running tests quite dramatically. Today I’m going to show you how to make your rspec experience super smooth.


The slowest part of running rspec tests is the initial task of loading of rails into memory. You have to do it every time you make a small change to your code and even for small projects it takes seconds.

Spring is a rails preloader that comes with Rails 4.1 by default, but you can use it with older versions (3.2+) as well, and it solves the loading time problem for you.

Installation is very easy, just add the following gems into your Gemfile.

group :development do
  gem 'spring'
  gem 'spring-commands-rspec'

Now, you can run bundle exec spring rspec and it keeps rails running in the background, so it won’t be necessary to load it every time.

~/projects/hubert [master] $ be rspec spec/models/group_randomizer_spec.rb

Finished in 0.53948 seconds (files took 7.03 seconds to load)
4 examples, 0 failures

~/projects/hubert [master] $ be spring rspec spec/models/group_randomizer_spec.rb

Finished in 0.57448 seconds (files took 0.71926 seconds to load)
4 examples, 0 failures

The benefit is amazing and on my underpowered netbook I’m now running tests nearly 10x faster.


The second tool that makes running rspec a joy is a tool called guard-rspec. Guard is a file watcher that runs certain actions when you change a file and guard-rspec is the one tailored for coping with rspec.

Installation is easy as well. Firstly, add it to your Gemfile.

group :development do
  gem 'guard-rspec'

After that, run initialization task to create a Guardfile by running this.

bundle exec guard init

Last step is to combine spring and guard together by modifying the Guardfile. Change the first line of guard :rspec command to look like the following.

guard :rspec, cmd: "bundle exec spring rspec" do

From now on, you can run it by executing

bundle exec guard

and every time you change your code it will run related tests. And it will do it blazingly fast thanks to the speed boost from spring.

Would you like to get the most interesting content about programming every Monday?
Sign up to Programming Digest and stay up to date!