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

发布于 2024-12-14 14:12:12 字数 3084 浏览 2 评论 0原文

当我运行 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

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

关于我可以尝试的其他事情有什么线索吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铜锣湾横着走 2024-12-21 14:12:12

为什么要运行rspec spec?通常要运行的是rake spec

尝试使用bundle exec rspec specbundle exec rake spec

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

另一件需要检查的事情,您的项目文件中的.rspec~/.rspec有什么奇怪的吗?

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?

水水月牙 2024-12-21 14:12:12

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

rspec --init

但运行失败

rails generate rspec:install

执行后者解决了问题。

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.

生生漫 2024-12-21 14:12:12

就我而言,

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

./Gemfile 中缺失。

In my case

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

was missing in ./Gemfile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文