6

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.

friartuck
  • 2,954
  • 4
  • 33
  • 67
  • show us your user model – Anthony Aug 13 '14 at 15:33
  • I suppose you do have a file `app/models/user.rb` with class `User` in it? – Kirti Thorat Aug 13 '14 at 15:34
  • 1
    yes app/models/user.rb contains an empty class: class User < ActiveRecord::Base end – friartuck Aug 13 '14 at 15:36
  • 1
    Just a terminology note: "Model" is a word Rails uses for Ruby classes that inherit from the class `ActiveRecord::Base`. All variables in Ruby that start with a capital letter are constants, so when you refer to `User`, Ruby looks for a constant with that name. That is to say, `class User < ActiveRecord::Base` creates a Ruby class that is a Rails model and assigns it to the constant `User`. – Jordan Running Aug 13 '14 at 15:38
  • As to your actual question: What are the contents of `spec_helper.rb`? Do you have specs for other models that *do* run? – Jordan Running Aug 13 '14 at 15:42
  • no, this is my first and only spec I've made. – friartuck Aug 13 '14 at 15:43
  • possible duplicate of [Rspec don't see my model Class. uninitialized constant error](http://stackoverflow.com/questions/17507416/rspec-dont-see-my-model-class-uninitialized-constant-error) – Kirti Thorat Aug 13 '14 at 15:56
  • I've just had a look through that, so I've run rails generate rspec:install, then replaced spec_helper.rb with the generated rails_helper.rb. I now get more errors, which I logged to disk. Here is the output http://pastebin.com/SyFpbw6v – friartuck Aug 13 '14 at 16:07
  • Check this out for reference : http://stackoverflow.com/questions/24176446/rspec-capybara-loading-in-progress-circular-require-considered-harmful – Kirti Thorat Aug 13 '14 at 16:15

3 Answers3

18

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
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
MrHello
  • 207
  • 2
  • 7
1

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.

friartuck
  • 2,954
  • 4
  • 33
  • 67
1

For me, the issue was that the name of the class was not being correctly called due to it being in another namespace. For example, I had:

RSpec.describe User do

  # ... some tests

end

where I should have had

RSpec.describe MyModule::User do

  # ... some tests

end

because the user class was defined with the MyModule module.