When trying to run rspec, I get "uninitialized constant ActiveModel"
Asked Answered
L

3

0

When I run rspec spec I get the following:

/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions/active_record/base.rb:26:in `': uninitialized constant ActiveModel (NameError)
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in `require'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in `'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in `require'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in `'
    from /Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in `require'
    from /Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in `'
    from /Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in `require'
    from /Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in `'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `load'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `block in load_spec_files'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `map'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `load_spec_files'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:18:in `run'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in `run_in_process'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:69:in `run'
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in `block in autorun'

My spec_helper.rb looks like this:

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require 'rspec/rails'
require 'rspec/autorun'
require 'spork'

Spork.prefork do
  ENV['RAILS_ENV'] ||= 'test'
  require File.expand_path('../../config/environment', __FILE__)
  require 'rspec/rails'

  RSpec.configure do |config|
    config.mock_with :rspec
    config.fixture_path = "#{Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true

    ActiveSupport::Dependencies.clear
  end
end

Spork.each_run do
  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/app/**/*.rb"].each {|f| load f}
end

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

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
end

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

If I delete my pages_controller_spec file the errors go away. But I have zero specs to run.

The error returns when I have something even as simple as this:

require 'spec_helper'

describe PagesController do
  it "should do something..."
end

Also, if I add any tests no matter where I get the same error.

Any clues on other things I can try?

Lickspittle answered 8/11, 2011 at 18:4 Comment(2)
Have you tried bundle exec guard start?Verrucose
@Verrucose that resulted in the same error.Lickspittle
D
5

Why are you running rspec spec? The typical thing to run is rake spec.

Try bundle exec rspec spec or bundle exec rake spec.

If that doesn't work, try to see if something is wrong with the rest of your environment -- try bundle exec rails console and bundle exec rails server

Another thing to check, is there anything odd in .rspec in your project file, or ~/.rspec?

Dynamo answered 9/11, 2011 at 5:33 Comment(5)
I reset up the whole rails project in a new project and that fixed it. The 'rspec spec' came from the Rails Tutorial by MH.Lickspittle
is this issue related to this question here: devnet.jetbrains.net/message/… ?Ney
I am encountering this same issue. @NoahClark, what exactly do you mean by "i reset up the whole rails project in a new project? Did you create a new project and just transfer the files one by one until it was working?Aground
@Aground yeah, that's probably what I did not sure as it's been a couple years. Might want to check your models as that seems to be the issue.Lickspittle
I figured out what it is. I had a duplicate line in my spec_helper from copying/pasting stuff into it and it caused that error.Aground
D
3

Found this thread while debugging the same symptoms. Here is another reason for the error: I had run

rspec --init

but had failed to run

rails generate rspec:install

Doing the latter fixed the problem.

Dantzler answered 6/4, 2013 at 9:58 Comment(1)
Same here, I had run rspec --init, but not rails generate rspec:install. The Rails generator includes Rails.root in the spec helper.Sleave
W
2

In my case

  gem "spork-rails", "~> 4.0.0"

was missing in ./Gemfile.

Whitehurst answered 12/1, 2014 at 23:56 Comment(1)
Also you must add ` gem 'spork', '~> 1.0.0rc4'` to your gemfile regardless of what SporkRails says. SporkRails will not find it by itself.Bidarka

© 2022 - 2024 — McMap. All rights reserved.