Pitfalls of Testing Scripts With Load - Part 1

Check out the full Testing Scripts with Load series: part 1, part 2, part 3

Previously it was shown how to use load to test scripts. As with all techniques, there are some drawbacks to using load.

Hidden Require Dependency

Take script:

1
2
3
4
5
6
7
8
#!/usr/bin/env ruby
$:.unshift File.join File.dirname(__FILE__), "..", "lib"

require 'local_server'

logger = Logger.new($stderr)

LocalServer.new(logger).run

and passing spec:

1
2
3
4
5
6
7
8
9
10
11
12
13
require 'spec_helper'
require 'logger'
require 'stringio'

describe 'Running the server locally' do
  it 'logs that it is running' do
    io = StringIO.new
    allow(Logger).to receive(:new).and_return(Logger.new io)

    expect{ load 'script/local-server' }.to change{ io.string }
      .to include 'SERVER: starting up'
  end
end

However, when the script is run standalone, it errors with:

1
uninitialized constant Logger (NameError)

Be aware that since load happens in the current spec context, a missing require may not be noticed if it is required by the spec.

Whenever possible have at least one test that shells out as a sanity check.