Jack comes to code

Ruby / Rails / Sinatra / APIs

Rspec in Turbo Mode: Setup Spork + Factory Girl + Rspec

Rspec is normally preferrable against Rails’ Test::Unit. However, developers can get anxious easily when running slow Rspec hundreds of time every day. It is slow because every time you start a test, even if there’s only one file, Rspec loans all files. Instead, Sport forks a copy of your Rails server on DRb server in the background, when you kick off your testing.

The basic installation and configuration can’t be simpler with the instruction from its README. However, people still find problems afterwards:

First, make sure you have turned off cache class in environment conf:

config/environments/test.rb
1
config.cache_classes = false

Second, clean dependencies at the end of Spork.prefork block every time you start Spork:

spec/spec_helper.rb
1
2
3
Spork.prefork do
  ActiveSupport::Dependencies.clear
end

Simply ask FactoryGirl to reload new fixture every time you run test

spec/spec_helper.rb
1
2
3
4
Spork.each_run do
  # This code will be run each time you run your specs.
  FactoryGirl.reload
end

Here we go! Spork also supports different Ruby frameworks and IDEs.