当试图运行rspec时,我得到“未初始化的常量ActiveModel”

时间:2011-11-08 18:04:05

标签: ruby-on-rails ruby testing rspec guard

当我运行rspec spec时,我得到以下内容:

/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'

我的spec_helper.rb看起来像这样:

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}

如果我删除了pages_controller_spec文件,则错误消失。但我没有规格可以运行。

当我有一个简单的东西时,错误就会返回:

require 'spec_helper'

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

另外,如果我添加任何测试,无论我在哪里得到相同的错误。

我可以尝试其他事情的线索吗?

3 个答案:

答案 0 :(得分:4)

为什么要运行rspec spec?典型的运行方式是rake spec

尝试bundle exec rspec specbundle exec rake spec

如果不起作用,请尝试查看环境其他部分是否有问题 - 尝试bundle exec rails consolebundle exec rails server

要检查的另一件事是,项目文件中的 .rspec 有什么奇怪的,或〜/ .rspec

答案 1 :(得分:3)

在调试相同症状时找到此线程。这是错误的另一个原因:我已经运行

rspec --init

但未能运行

rails generate rspec:install

后者解决了这个问题。

答案 2 :(得分:2)

就我而言

  gem "spork-rails", "~> 4.0.0"
./Gemfile中缺少