rspec/rspec-core — Issue #483: Prevent "require" in "spec_helper" from polluting code's namespace
Author: e2
Created: 2011-10-21 09:21:07+00:00
State: open

Example is the best explanation:

*spec_helper.rb*:

require 'simplecov' # has "require 'erb'" somewhere
require 'rspec'

*foo_spec.rb*:

require 'foo'
describe "Foo" do
it "should evaluate code" do
Foo.eval('1+1').should == '2'
end
end

*foo.rb*:

# BUG: developer forgot "require 'erb'" here ...

def eval(code)
# NameError for "ERB" raised in production only!
ERB.new("<%=#{code}%>").result
end

Result: Specs pass, project is deployed and "NameError" occurs in production

Expected: Specs should fail with "NameError" exception.

Description: things like ERB, FileUtils,etc shouldn't be available for the code being tested, unless it is explicitly loaded by that code.

I would write a patch, I'm just making sure I'm not missing something important here.