Search code examples
ruby-on-rails-4rspec-rails

Rails + RSpec - spec/support files not being required; giving "Uninitialized Constant (NameError)"


In my current project, I am not able to run any of the test case because I am getting the following error:

/home/harsh/Documents/viburnix/cloud/viburnix/spec/support/helpers.rb:2:in `block in <top (required)>': uninitialized constant Features (NameError)

Full trace:

/home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/actionpack-4.1.1/lib/action_dispatch/http/mime_type.rb:163: warning: already initialized constant Mime::ZIP
/home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/actionpack-4.1.1/lib/action_dispatch/http/mime_type.rb:163: warning: previous definition of ZIP was here
Coverage report generated for RSpec to /home/harsh/Documents/viburnix/cloud/viburnix/coverage. 11 / 56 LOC (19.64%) covered.
/home/harsh/Documents/viburnix/cloud/viburnix/spec/support/helpers.rb:2:in `block in <top (required)>': uninitialized constant Features (NameError)
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core.rb:112:in `configure'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/support/helpers.rb:1:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/spec_helper.rb:16:in `block in <top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/spec_helper.rb:16:in `each'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/spec_helper.rb:16:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/models/user_spec.rb:1:in `require'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/models/user_spec.rb:1:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `load'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `block in load_spec_files'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `each'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `load_spec_files'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/command_line.rb:21:in `run'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/runner.rb:100:in `run'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/runner.rb:31:in `invoke'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/exe/rspec:4:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/bin/rspec:23:in `load'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/bin/rspec:23:in `<main>'

spec/spec_helper.rb is the standard file generated by spec:install and includes:

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

In spec/support/helpers.rb, I have:

RSpec.configure do |config|
  config.include Features::SessionHelpers, type: :feature
  config.include Mongoid::Matchers, type: :model
  config.include AuthenticationHelpers, type: :controller
end

And it gives error on line 2: Features::SessionHelpers.

Seems like it is either not requiring the code at all or just not loading in correct order.

The problem is that in a team of 7, I am the only one who is not able to run spec. Any help or direction is much needed and would be appreciated.


Solution

  • Sorry for late reply. This project was started by another team and yes all the files, modules and classes were present at right place. The problem was the sequence of loading/requiring (in spec_helper.rb) these files. While other people were working happily without any issue, I had to load/require some of the required files earlier.

    What baffles me are 2 questions:

    1. I am not puzzled it didn't work for me. The requiring of those specific files manually make sense and should have been that way from the start. I am puzzled at how did it work for other people?
    2. I tried making a virtual machine exactly the same way as other people. Yet I faced the issue, debunking the myth that this has to do with OS or shell etc.

    I have added those lines to spec_helper.rb and I comment them after running tests but before committing my work.