RSpec load_missing_constant, expected X to define Y (and it does)

rspec, ruby, ruby-on-rails

Solution

When your write Links::Category in your spec, Rails probably auto-loads the `Links` module and `Links::Category` class.

Apparently this strange error can happen if you are not properly defining the `Links` module. See the comments above for more details about how we debugged this.

Problem

When we run `bundle exec rake spec` it errors while trying to load the environment with the error: ``` ...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) ``` The file `app/models/links/Category.rb` does indeed define `Links::Category`. Even stranger is that error does not occur when running under guard and spork (the standard way we run tests): `bundle exec guard -i` Runs the test suite as expected without issue. Spork is configured to run rspec, so I'm a bit confused about why running `rake spec` manually would cause this. I've seen similar issues which seemed to be solved by looking at the `autoload_paths`, and checking if it was including `lib` and `lib/**` however ours isn't doing anything funky with autoload_paths that I can see. Our `autoload_paths` looks like this (defined in application.rb): ``` config.autoload_paths += %W(#{Rails.root}/app/src #{config.root}/app/api #{Rails.root}/app/products) ``` (We're using `bundle exec rake spec` in our CI server to run the tests rather than guard which we use on our development machines). Does category.rb get loaded Added `puts 'Hey'` at the top of category.rb, and `puts 'Yo'` at the bottom, and when running the specs the output incldues it: ``` 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) ``` app/models/links/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 ``` app/models/links/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 ``` **Simplest category_spec.rb that causes error*: ``` require 'spec_helper' describe Links::Category do pending "add some examples to (or delete) #{__FILE__}" end ``` Rake execution: ``` ➜ 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 ```

Original source