05 December 2009

require ‘spec/spec_helper’

This has been ranted about by others many times and I never really understood the big deal. Now I do, especially after it shaved 5 seconds off our suite of specs.

After some research I found out we were requiring our spec helpers in a “bad way.” Most of this is because of my own doing, but I thought I’d share with you guys.

There were probably 4 or 5 variations of the syntax used to require spec_helper.

require File.dirname(__FILE__) + /../spec_helper
require File.join(File.dirname(__FILE__), .., spec_helper)
require File.expand_path(File.dirname(__FILE__), + /../spec_helper)
require File.expand_path(File.join(File.dirname(__FILE__), .., spec_helper))

Each of these variations existed for the different possible depths below spec, for example, spec/models, spec/models/lead, etc.

This causes ruby to attempt to load the file each time, which we really don’t want to do.

The solution is simple.

require spec/spec_helper

This works with autospec and rake just fine and looks much better.

Filed under ruby testing