ruby-on-rails – RSpec load_missing_constant,期望X定义Y(并且确实如此)

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – RSpec load_missing_constant,期望X定义Y(并且确实如此)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们跑的时候

捆绑exec rake spec

尝试使用错误加载环境时出错:

...gems/activesupport-3.2.8/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected ...app/models/links/category.rb to define Links::Category (LoadError)

文件app / models / links / Category.rb确实定义了Links :: Category.更奇怪的是,当在防守和spork(我们运行测试的标准方式)下运行时不会发生错误

捆绑执行警卫-i

按预期运行测试套件没有问题.

Spork配置为运行rspec,所以我有点困惑为什么手动运行rake规范会导致这种情况.

我已经看到类似的问题似乎通过查看autoload_paths来解决,并检查它是否包含lib和lib / **但是我们可以看到autoload_paths没有做任何时髦的事情.我们的autoload_paths看起来像这样(在application.rb中定义):

config.autoload_paths += %W(#{Rails.root}/app/src #{config.root}/app/api #{Rails.root}/app/products)

(我们在CI服务器中使用bundle exec rake规范来运行测试而不是我们在开发机器上使用的防护).

category.rb是否已加载

将’Hey’添加到category.rb的顶部,并将’Yo’放在底部,当运行规范时,输出包含它:

DEPRECATION WARNING: ActiveAdmin::Dashboard is deprecated and will be removed in the next version
Hey
Yo
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /Users/shimms/Development/lexim/app/models/links/category.rb to define Links::Category (LoadError)

应用程序/模型/链接/ link.rb:

class Links::Link < ActiveRecord::Base
  self.table_name = 'links_category_links'
  attr_accessible :description,:name,:url,:category_id

  acts_as_paranoid

  belongs_to :category,:class_name => 'Links::Category'

  validates_presence_of :url
  validates_presence_of :name
end

应用程序/模型/链接/ category.rb:

class Links::Category < ActiveRecord::Base
  self.table_name = 'links_categories'
  attr_accessible :description,:space_id

  acts_as_paranoid

  extend FriendlyId
  friendly_id :name,:use => :scoped,:scope => :space

  belongs_to :space
  belongs_to_space(:space)
  has_many :links,:class_name => 'Links::Link'

  validates_presence_of :name
end

spec_helper.rb

require File.expand_path('../../config/environment',__FILE__)
require 'rubygems'
require 'rails/all'
require 'rspec/rails'
require 'factory_girl'

system "#{Rails.root.to_s}/db/test.db"

ENV['RAILS_ENV'] ||= 'test'

load_schema = lambda {
  load "#{Rails.root.to_s}/db/schema.rb" # use db agnostic schema by default
                                         # ActiveRecord::Migrator.up('db/migrate') # use migrations
}
silence_stream(STDOUT,&load_schema)

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

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

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

  ActiveSupport::Dependencies.clear
end

**最简单的category_spec.rb导致错误*:

require 'spec_helper'

describe Links::Category do
  pending "add some examples to (or delete) #{__FILE__}"
end

耙子执行:

➜  lexim git:(deveop) ✗ RAILS_ENV=test be rake spec --trace
** Invoke spec (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
Agent is configured to send raw sql to the service
Agent is configured to send raw sql to the service
DEPRECATION WARNING: Passing :tag,:class and others to use is deprecated. Please invoke b.use :input,:wrap_with => {:class=>"span4"} instead. (called from block (2 levels) in <top (required)> at /Users/shimms/Development/lexim/config/initializers/simple_form.rb:47)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:test:purge
** Execute db:test:load
** Invoke db:test:load_schema (first_time)
** Invoke db:test:purge
** Execute db:test:load_schema
** Invoke db:schema:load (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:load
** Execute spec
/Users/shimms/.rvm/rubies/ruby-1.9.3-p125/bin/ruby -S rspec ./spec/product_features_spec.rb ./spec/controllers/lexim/logo_controller_spec.rb ./spec/mailers/enterprise_enquiry_mailer_spec.rb ./spec/mailers/new_account_mailer_spec.rb ./spec/mailers/user_mailer_spec.rb ./spec/models/account_boltons_spec.rb ./spec/models/account_spec.rb ./spec/models/account_status_spec.rb ./spec/models/address_spec.rb ./spec/models/admin/import_spec.rb ./spec/models/admin_user_spec.rb ./spec/models/assignment_materials_spec.rb ./spec/models/assignment_submission_rubric_selection_spec.rb ./spec/models/assignments/submission_files_spec.rb ./spec/models/assignments/submission_spec.rb ./spec/models/bolton_screenshot_spec.rb ./spec/models/bolton_spec.rb ./spec/models/bolton_status_spec.rb ./spec/models/calendar/entry_spec.rb ./spec/models/calendar/event_sharing_spec.rb ./spec/models/calendar_spec.rb ./spec/models/cancellation_reason_spec.rb ./spec/models/charge_spec.rb ./spec/models/client_application_spec.rb ./spec/models/common_cartridge/export_spec.rb ./spec/models/credit_card_spec.rb ./spec/models/discussions/reply_spec.rb ./spec/models/discussions/tag_spec.rb ./spec/models/education_domain_name_spec.rb ./spec/models/email_address_validation_spec.rb ./spec/models/gradebook/column_spec.rb ./spec/models/gradebook/value_spec.rb ./spec/models/gradebook_column_type_spec.rb ./spec/models/importer/user_spec.rb ./spec/models/invitation_spec.rb ./spec/models/invoice_spec.rb ./spec/models/links/category_spec.rb ./spec/models/links/link_spec.rb ./spec/models/mailing_list_spec.rb ./spec/models/material_item_spec.rb ./spec/models/materials/folder_spec.rb ./spec/models/materials/page_details_spec.rb ./spec/models/materials/page_spec.rb ./spec/models/materials/youtube_details_spec.rb ./spec/models/mobile_phone_country_carrier_spec.rb ./spec/models/mobile_phone_country_spec.rb ./spec/models/net_promoter_score_spec.rb ./spec/models/network_spec.rb ./spec/models/notification_channel_spec.rb ./spec/models/notification_event_spec.rb ./spec/models/notification_spec.rb ./spec/models/open_graph_object_authorization_spec.rb ./spec/models/plan_pool_spec.rb ./spec/models/plan_spec.rb ./spec/models/privacy_setting_spec.rb ./spec/models/product_bolton_spec.rb ./spec/models/product_spec.rb ./spec/models/quiz_spec.rb ./spec/models/quizzes/question_option_spec.rb ./spec/models/quizzes/question_spec.rb ./spec/models/quizzes/question_type_spec.rb ./spec/models/quizzes/submission_answer_option_spec.rb ./spec/models/quizzes/submission_answer_spec.rb ./spec/models/quizzes/submission_spec.rb ./spec/models/referrer_campaign_spec.rb ./spec/models/registration_spec.rb ./spec/models/rubric_spec.rb ./spec/models/rubrics/criterion_spec.rb ./spec/models/rubrics/descriptor_spec.rb ./spec/models/rubrics/level_spec.rb ./spec/models/sanitized_text_spec.rb ./spec/models/scheduled_mail_spec.rb ./spec/models/space_spec.rb ./spec/models/subscription_spec.rb ./spec/models/subscription_status_spec.rb ./spec/models/system_announcement_spec.rb ./spec/models/system_announcement_views_spec.rb ./spec/models/tip_spec.rb ./spec/models/tour_activity_spec.rb ./spec/models/unauthorized_access_spec.rb ./spec/models/unavailable_feature_request_spec.rb ./spec/models/user_activity_spec.rb ./spec/models/user_authentication_tokens_spec.rb ./spec/models/user_creation_type_spec.rb ./spec/models/user_notification_spec.rb ./spec/models/user_spec.rb ./spec/models/wall_post_likes_spec.rb ./spec/models/wallpost_spec.rb
Agent is configured to send raw sql to the service
Agent is configured to send raw sql to the service
DEPRECATION WARNING: Passing :tag,:wrap_with => {:class=>"span4"} instead. (called from block (2 levels) in <top (required)> at /Users/shimms/Development/lexim/config/initializers/simple_form.rb:47)
DEPRECATION WARNING: ActiveAdmin::Dashboard is deprecated and will be removed in the next version
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /Users/shimms/Development/lexim/app/models/links/category.rb to define Links::Category (LoadError)
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `each'
from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `const_missing'
    from /Users/shimms/Development/lexim/spec/models/links/category_spec.rb:4:in `<top (required)>'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `block in load'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
    from /Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'
rake aborted!
/Users/shimms/.rvm/rubies/ruby-1.9.3-p125/bin/ruby -S rspec ./spec/product_features_spec.rb ./spec/controllers/lexim/logo_controller_spec.rb ./spec/mailers/enterprise_enquiry_mailer_spec.rb ./spec/mailers/new_account_mailer_spec.rb ./spec/mailers/user_mailer_spec.rb ./spec/models/account_boltons_spec.rb ./spec/models/account_spec.rb ./spec/models/account_status_spec.rb ./spec/models/address_spec.rb ./spec/models/admin/import_spec.rb ./spec/models/admin_user_spec.rb ./spec/models/assignment_materials_spec.rb ./spec/models/assignment_submission_rubric_selection_spec.rb ./spec/models/assignments/submission_files_spec.rb ./spec/models/assignments/submission_spec.rb ./spec/models/bolton_screenshot_spec.rb ./spec/models/bolton_spec.rb ./spec/models/bolton_status_spec.rb ./spec/models/calendar/entry_spec.rb ./spec/models/calendar/event_sharing_spec.rb ./spec/models/calendar_spec.rb ./spec/models/cancellation_reason_spec.rb ./spec/models/charge_spec.rb ./spec/models/client_application_spec.rb ./spec/models/common_cartridge/export_spec.rb ./spec/models/credit_card_spec.rb ./spec/models/discussions/reply_spec.rb ./spec/models/discussions/tag_spec.rb ./spec/models/education_domain_name_spec.rb ./spec/models/email_address_validation_spec.rb ./spec/models/gradebook/column_spec.rb ./spec/models/gradebook/value_spec.rb ./spec/models/gradebook_column_type_spec.rb ./spec/models/importer/user_spec.rb ./spec/models/invitation_spec.rb ./spec/models/invoice_spec.rb ./spec/models/links/category_spec.rb ./spec/models/links/link_spec.rb ./spec/models/mailing_list_spec.rb ./spec/models/material_item_spec.rb ./spec/models/materials/folder_spec.rb ./spec/models/materials/page_details_spec.rb ./spec/models/materials/page_spec.rb ./spec/models/materials/youtube_details_spec.rb ./spec/models/mobile_phone_country_carrier_spec.rb ./spec/models/mobile_phone_country_spec.rb ./spec/models/net_promoter_score_spec.rb ./spec/models/network_spec.rb ./spec/models/notification_channel_spec.rb ./spec/models/notification_event_spec.rb ./spec/models/notification_spec.rb ./spec/models/open_graph_object_authorization_spec.rb ./spec/models/plan_pool_spec.rb ./spec/models/plan_spec.rb ./spec/models/privacy_setting_spec.rb ./spec/models/product_bolton_spec.rb ./spec/models/product_spec.rb ./spec/models/quiz_spec.rb ./spec/models/quizzes/question_option_spec.rb ./spec/models/quizzes/question_spec.rb ./spec/models/quizzes/question_type_spec.rb ./spec/models/quizzes/submission_answer_option_spec.rb ./spec/models/quizzes/submission_answer_spec.rb ./spec/models/quizzes/submission_spec.rb ./spec/models/referrer_campaign_spec.rb ./spec/models/registration_spec.rb ./spec/models/rubric_spec.rb ./spec/models/rubrics/criterion_spec.rb ./spec/models/rubrics/descriptor_spec.rb ./spec/models/rubrics/level_spec.rb ./spec/models/sanitized_text_spec.rb ./spec/models/scheduled_mail_spec.rb ./spec/models/space_spec.rb ./spec/models/subscription_spec.rb ./spec/models/subscription_status_spec.rb ./spec/models/system_announcement_spec.rb ./spec/models/system_announcement_views_spec.rb ./spec/models/tip_spec.rb ./spec/models/tour_activity_spec.rb ./spec/models/unauthorized_access_spec.rb ./spec/models/unavailable_feature_request_spec.rb ./spec/models/user_activity_spec.rb ./spec/models/user_authentication_tokens_spec.rb ./spec/models/user_creation_type_spec.rb ./spec/models/user_notification_spec.rb ./spec/models/user_spec.rb ./spec/models/wall_post_likes_spec.rb ./spec/models/wallpost_spec.rb Failed
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/rake_task.rb:137:in `block (2 levels) in initialize'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:60:in `verbose'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rspec-core-2.10.1/lib/rspec/core/rake_task.rb:127:in `block in initialize'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/shimms/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/bin/rake:23:in `load'
/Users/shimms/.rvm/gems/ruby-1.9.3-p125@lexim/bin/rake:23:in `<main>'
Tasks: TOP => spec

解决方法

当您在规范中编写Links :: Category时,Rails可能会自动加载Links模块和Links :: Category类.

显然,如果您没有正确定义Links模块,可能会发生这种奇怪的错误.有关我们如何调试此内容的更多详细信息,请参阅上面的注释.

原文链接:https://www.f2er.com/ruby/269755.html

猜你在找的Ruby相关文章