8

当我们跑

bundle 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. 更奇怪的是,在 guard 和 spork(我们运行测试的标准方式)下运行时不会发生错误:

bundle exec guard -i

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

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

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

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

(我们bundle exec rake spec在 CI 服务器中使用来运行测试,而不是我们在开发机器上使用的守卫)。

category.rb 是否被加载

添加puts 'Hey'在 category.rb 的顶部和puts '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, :name, :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, :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)
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
4

1 回答 1

4

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

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

于 2012-11-19T07:58:47.287 回答