Fast and fancy spec with spring + spectator

Several months ago I’ve started using the spectator gem. I’m using the Rspec testing framework with the spectator event watcher as the test runner. Spring allows us to watch the test run almost immediately after hitting save of a file in the editor.

In my experience spectator is easy to to use, you don’t need to configure anything, it just works out of the box. Tools like guard on the other hand have long list of options to run with.

Using spectator is pretty simple, just install the spectator gem gem install spectator and then run spectator in your root project. Now every time you modify a file in your project, spectator searches and launches the associated spec.

Our goal is to integrate spring in test environment in order to improve startup performance and thus speed up the entire process.

My current gemfile setup icludes:

# Gemfile

group :development, :test do
  gem 'rspec-rails', '~> 3.0'
  gem 'spring'
end

group :development
    gem 'spring-commands-rspec'
    gem 'spectator'
end

spring-commands-rspec implements the rspec command for Spring. As you can see in spring-commands-rspec readme, this gem generates a bin/rspec file like this:

# bin/rspec

#!/usr/bin/env ruby
begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require 'bundler/setup'
load Gem.bin_path('rspec-core', 'rspec')

To verify that the installation of spring-commands-rspec is successful you can run ./bin/rspec and then verify that spring is running with spring status.
The first startup will take a moment. Now that spring has started the app server for you, subsequent runs will use a forked process from this server instead and return faster.

To integrate faster ./bin/rspec command in spectator just create a .spectator.rb file in your root project as above:

# .spectator.rb

ENV['RSPEC_COMMAND'] = './bin/rspec'

Have a nice and fast TDD!

Leave a Reply

wpDiscuz