Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing to run RSpec (uninitialized constant User (NameError)

Trying to run bundle exec rspec spec/models/user_spec.rb but failing to execute (see error below).

Contents of user_spec.rb:

require 'rails_helper'

describe User do
  pending "add some examples to (or delete) #{__FILE__}"
end

If I remove the last 3 lines, then it complete with 0 examples and 0 failures. However, when the last 3 lines are present, it generates an error

/spec/models/user_spec.rb:4:in `<top (required)>': uninitialized constant User (NameError)
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `block in load_spec_files'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `each'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load_spec_files'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:97:in `setup'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:85:in `run'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:70:in `run'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:38:in `invoke'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/exe/rspec:4:in `<top (required)>'
    from /usr/local/bin/rspec:23:in `load'
    from /usr/local/bin/rspec:23:in `<main>'

It seems it doesn't know what User is and treats it as a constant, when this is infact a model. I have verified in the ruby sandbox that I can create new Users in the database. Any ideas?


Answer: After removing --warnings from .rspec (thank you kirti), current error was about capybara version being too low (requires 2.2). After installing the latest version (modifying gemfile and bundle install), rspec appears to be executing correctly. Thank you to all for your comments.

like image 573
friartuck Avatar asked Aug 13 '14 15:08

friartuck


1 Answers

I had the same issue, this was due to rspec/rails_helper.rb not being called anywhere.

I added it to the .rspec file, so my file look like this

--color
--require spec_helper
--require rails_helper

This and adding warnings to false in the spec/spec_helper.rb solved it for me !

  config.warnings = false
like image 105
MrHello Avatar answered Sep 20 '22 19:09

MrHello