Chapter 4. Setting Up and Cleaning Up

This chapter discusses support code to set tests up and clean up after them. Initialization, configuration, cleanup, and other support code related to RSpec specs are important in real-world RSpec usage. We will learn how to cleanly organize support code in real-world applications by learning about the following topics:

  • Configuring RSpec with spec_helper.rb
  • Initialization and configuration of resources
  • Preventing tests from accessing the Internet with WebMock
  • Maintaining clean test state
  • Custom helper code
  • Loading support code on demand with tags

Configuring RSpec with spec_helper.rb

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

To see what these two options do, let's create a couple of dummy spec files that include our spec_helper.rb. Here's the first spec file:

# spec/first_spec.rb
require 'spec_helper'

describe 'first spec' do
  it 'sleeps for 1 second' do
    sleep 1
  end
  
  it 'sleeps for 2 seconds' do
    sleep 2
  end  
  
  it 'sleeps for 3 seconds' do
    sleep 3
  end  
end

And here's our second spec file:

# spec/second_spec.rb
require 'spec_helper'

describe 'second spec' do
  it 'sleeps for 4 second' do
    sleep 4
  end
  
  it 'sleeps for 5 seconds' do
    sleep 5
  end  
  
  it 'sleeps for 6 seconds' do
    sleep 6
  end  
end

Now let's run our two spec files and see what happens:

Configuring RSpec with spec_helper.rb

We note that we used --format documentation when running RSpec so that we see the order in which the tests were run (the default format just outputs a green dot for each passing test). From the output, we can see that the tests were run in a random order. We can also see the three slowest specs.

Although this was a toy example, I would recommend using both of these configuration options for RSpec. Running examples in a random order is very important, as it is the only reliable way of detecting bad tests which sometimes pass and sometimes fail based on the order that the overall test suite is run. Also, keeping tests running fast is very important for maintaining a productive development flow, and seeing which tests are slow on every test run is the most effective way of encouraging developers to make the slow tests fast, or remove them from the test run.

We'll return to both test order and test speed later. For now, let us just note that RSpec configuration is very important to keeping our specs reliable and fast.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset