gpt4 book ai didi

ruby-on-rails - 名称错误 : uninitialized constant in Rails app when testing with RSpec and Spork

转载 作者:行者123 更新时间:2023-12-04 06:22:55 26 4
gpt4 key购买 nike

我正在尝试在终端中使用以下命令在我的第一个 Rails 应用程序中运行我的测试:

rspec spec/

但这只会导致以下错误:

Exception encountered: #<NameError: uninitialized constant PagesController>
backtrace:
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-expectations-2.3.0/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
/Users/rich/dev/rails_projects/abunchofletters/spec/controllers/pages_controller_spec.rb:3:in `<top (required)>'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/configuration.rb:388:in `block in load_spec_files'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/configuration.rb:388:in `map'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/configuration.rb:388:in `load_spec_files'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/command_line.rb:18:in `run'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/monkey/spork/test_framework/rspec.rb:4:in `run_tests'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/spork-0.9.0.rc2/lib/spork/run_strategy/forking.rb:13:in `block in run'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/spork-0.9.0.rc2/lib/spork/forker.rb:21:in `block in initialize'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/spork-0.9.0.rc2/lib/spork/forker.rb:18:in `fork'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/spork-0.9.0.rc2/lib/spork/forker.rb:18:in `initialize'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/spork-0.9.0.rc2/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/spork-0.9.0.rc2/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/spork-0.9.0.rc2/lib/spork/server.rb:47:in `run'
/Users/rich/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/drb/drb.rb:1558:in `perform_without_block'
/Users/rich/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/drb/drb.rb:1518:in `perform'
/Users/rich/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop'
/Users/rich/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop'

我可以在浏览器中成功运行该站点,并且“Spork 已准备好并正在监听”,这让我觉得我正在运行的 Gems、RSpec、Spork 或...有问题?而不是我编写的测试,因为 PagesController 和 RSpec 测试是基于 RailsTutorial.org 的书。我很茫然。

这是我的 Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
gem 'gravatar_image_tag', '0.1.0'
gem "nokogiri", '1.4.4'
gem 'will_paginate', '3.0.pre2'

group :development do
gem 'rspec-rails', '2.3.0'
gem 'annotate-models', '1.0.4'
gem 'faker', '0.3.1'
end

group :test do
gem 'rspec', '2.3.0'
gem 'webrat', '0.7.1'
gem 'spork', '0.9.0.rc2'
gem 'factory_girl_rails', '1.0'
end

后面是我的本地 gem 列表:

abstract (1.0.0)
actionmailer (3.0.3)
actionpack (3.0.3)
activemodel (3.0.3)
activerecord (3.0.3)
activeresource (3.0.3)
activesupport (3.0.3)
annotate-models (1.0.4)
arel (2.0.6)
autotest (4.3.2)
autotest-fsevent (0.2.2)
autotest-growl (0.2.4)
autotest-rails-pure (4.1.0)
builder (2.1.2)
bundler (1.0.3)
diff-lcs (1.1.2)
erubis (2.6.6)
factory_girl (1.3.2)
factory_girl_rails (1.0)
faker (0.3.1)
gravatar_image_tag (0.1.0)
i18n (0.5.0)
mail (2.2.12)
mime-types (1.16)
nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack-test (0.5.6)
rails (3.0.3)
railties (3.0.3)
rake (0.8.7)
rdoc (2.5.11)
rspec (2.3.0)
rspec-core (2.3.0)
rspec-expectations (2.3.0)
rspec-mocks (2.3.0)
rspec-rails (2.3.0)
spork (0.9.0.rc2)
sqlite3-ruby (1.2.5)
sys-uname (0.8.4)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.23)
webrat (0.7.1)
will_paginate (3.0.pre2)

有没有人有任何想法或者我可以通过提供更多信息来提供帮助?我是一个 Rails 新手,时不时地回来使用它,然后似乎在再次放弃之前遇到问题......

更新我的 Spec/spec_helper.rb 文件:

require 'rubygems'
require 'spork'

Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] ||= 'test'
unless defined?(Rails)
require File.dirname(__FILE__) + "/../config/environment"
end
require 'rspec/rails'

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

Rspec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec

config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

### Part of a Spork hack. See http://bit.ly/arY19y
# Emulate initializer set_clear_dependencies_hook in
# railties/lib/rails/application/bootstrap.rb
ActiveSupport::Dependencies.clear
end
end

Spork.each_run do
end

这是删除 spec_helper 的内容后终端的输出:

No DRb server is running. Running in local process instead ...
/Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/backward_compatibility.rb:20:in `const_missing': uninitialized constant Object::PagesController (NameError)
from /Users/rich/dev/rails_projects/abunchofletters/spec/controllers/pages_controller_spec.rb:3:in `<top (required)>'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/configuration.rb:388:in `load'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/configuration.rb:388:in `block in load_spec_files'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/configuration.rb:388:in `map'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/configuration.rb:388:in `load_spec_files'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/command_line.rb:18:in `run'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/runner.rb:55:in `run_in_process'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/runner.rb:44:in `run'
from /Users/rich/.rvm/gems/ruby-1.9.2-p0@abunchofletters/gems/rspec-core-2.3.0/lib/rspec/core/runner.rb:10:in `block in autorun'

最佳答案

事实证明,问题是由不同模型中错误的验证规则引起的。它花了很多随机的想法和尝试,但最终在错误消息的帮助下没有太多帮助。

关于ruby-on-rails - 名称错误 : uninitialized constant in Rails app when testing with RSpec and Spork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4479136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com