ruby-on-rails – NameError:使用RSpec和Spork进行测试时Rails应用程序中的未初始化常量

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – NameError:使用RSpec和Spork进行测试时Rails应用程序中的未初始化常量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在终端中使用以下命令在我的第一个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

其次是我的本地宝石列表:

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_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/717755.html

猜你在找的Ruby相关文章