The RSpec specs that we've seen so far have functioned as standalone units. Specs in the real world, however, almost never work without supporting code to prepare the test environment before tests are run and ensure it is cleaned up afterwards. In fact, the first line of nearly every real-world RSpec spec file loads a file that takes care of initialization, configuration, and cleanup:
require 'spec_helper'
By convention, the entry point for all support code for specs is in a file called spec_helper.rb
. Another convention is that specs are located in a folder called spec
in the root folder of the project. The spec_helper.rb
file is located in the root of this spec
folder.
Now that we know where it goes, what do we actually put in spec_helper.rb
? Let's start with an example:
# spec/spec_helper.rb require 'rspec' RSpec.configure do |config| config.order = 'random' config.profile_examples = 3 end