Having a `Test` class or model causes RSpec to fail
Moved from rspec/rspec-core#1859 opened by @zedtux
RSpec doesn't allow us to have a class named Test
while it should:
app/models/test.rb:6:in `<top (required)>': Test is not a class (TypeError)
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /cursus-scholar/application/spec/models/test_spec.rb:2:in `<top (required)>'
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
from /usr/local/bin/rspec:23:in `load'
from /usr/local/bin/rspec:23:in `<main>'
Steps to Reproduce
Here's a simple working spec with only RSpec:
class Test; end
RSpec.describe Test do
it "works" do
expect(described_class).to be Test
end
end
Now if I put that in a Rails project and load require 'rails_helper'
at the top you get the stack trace:
/example_app/spec/test_spec.rb:3:in `<top (required)>': Test is not a class (TypeError)
from /rspec-core/lib/rspec/core/configuration.rb:1200:in `load'
from /rspec-core/lib/rspec/core/configuration.rb:1200:in `block in load_spec_files'
from /rspec-core/lib/rspec/core/configuration.rb:1198:in `each'
from /rspec-core/lib/rspec/core/configuration.rb:1198:in `load_spec_files'
from /rspec-core/lib/rspec/core/runner.rb:97:in `setup'
from /rspec-core/lib/rspec/core/runner.rb:85:in `run'
from /rspec-core/lib/rspec/core/runner.rb:70:in `run'
from /rspec-core/lib/rspec/core/runner.rb:38:in `invoke'
from /rspec-core/exe/rspec:4:in `<top (required)>'
from bin/rspec:16:in `load'
from bin/rspec:16:in `<main>'
This is failing before it even parses the spec. It fails on the line class Test; end
.
Cause
This is happening because Test
is already defined and it's a module. It's coming from Test::Unit
.
Flipping the loading for Rails and rspec-rails with the Test
class definition it seems rspec-rails raises the same error (since Test
is already defined as a class) in lib/rspec/rails/matchers.rb
:
begin
require 'test/unit/assertionfailederror'
rescue LoadError
module Test
module Unit
class AssertionFailedError < StandardError
end
end
end
end