ruby - NameError: uninitialized constant when trying to execute rails tests -
in rails 3.2.13 app i'm writing right have model called business
. model getting fatter , fatter, decided split in two, extracting code related availability rails concern named availability
, stored @ models/concerns/business
class business module availability extend activesupport::concern availability_open = 1 availability_closed = -1 availability_complete = -2 ...
as concerns not enabled in rails default, i've put following line in config/application.rb
in order autoload them:
config.autoload_paths += %w(#{config.root}/app/models/concerns)
the problem that, after split, tests have stopped working, returning nameerror: uninitialized constant availability
error. suppose need require concern somehow in tests, haven't managed yet, , don't understand why concern needed in tests doesn't make use of it.
the following stack trace returned after running tests:
nameerror: uninitialized constant availability (erb):9:in `<main>' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/erb.rb:849:in `eval' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/erb.rb:849:in `result' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures/file.rb:51:in `render' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures/file.rb:43:in `rows' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures/file.rb:29:in `each' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:670:in `block (2 levels) in read_fixture_files' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures/file.rb:20:in `open' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:669:in `block in read_fixture_files' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:668:in `each' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:668:in `read_fixture_files' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:548:in `initialize' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:482:in `new' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:482:in `block (2 levels) in create_fixtures' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:479:in `map' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:479:in `block in create_fixtures' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:232:in `disable_referential_integrity' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:476:in `create_fixtures' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:895:in `load_fixtures' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:849:in `setup_fixtures' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:407:in `_run__871072887135343583__setup__3260066542044458782__callbacks' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks' /home/sergio/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/testing/setup_and_teardown.rb:35:in `run' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:919:in `block in _run_suite' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:912:in `map' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:912:in `_run_suite' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:657:in `block in _run_suites' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:655:in `each' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:655:in `_run_suites' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:867:in `_run_anything' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1060:in `run_tests' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1047:in `block in _run' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1046:in `each' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1046:in `_run' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1035:in `run' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:21:in `run' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:774:in `run' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:366:in `block (2 levels) in autorun' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:27:in `run_once' /home/sergio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/test/unit.rb:365:in `block in autorun'
update
this excerpt of class business
, includes availability
concern:
class business < activerecord::base include availability include emailvirtualattribute include telephonevirtualattribute belongs_to :place has_many :businesses_users has_many :users, :through => :businesses_users, :uniq => true has_many :customers, :inverse_of => :business, :dependent => :destroy has_many :emails, :as => :contactable, :class_name => 'email' has_many :telephones, :as => :contactable, :class_name => 'telephone' ...
the development/test configs standard ones, no changes have been made
test.rb
rendezvous::application.configure # settings specified here take precedence on in config/application.rb # test environment used exclusively run application's # test suite. never need work otherwise. remember # test database "scratch space" test suite , wiped # , recreated between test runs. don't rely on data there! config.cache_classes = true # configure static asset server tests cache-control performance config.serve_static_assets = true config.static_cache_control = "public, max-age=3600" # log error messages when accidentally call methods on nil config.whiny_nils = true # show full error reports , disable caching config.consider_all_requests_local = true config.action_controller.perform_caching = false # raise exceptions instead of rendering exception templates config.action_dispatch.show_exceptions = false # disable request forgery protection in test environment config.action_controller.allow_forgery_protection = false # tell action mailer not deliver emails real world. # :test delivery method accumulates sent emails in # actionmailer::base.deliveries array. config.action_mailer.delivery_method = :test # raise exception on mass assignment protection active record models config.active_record.mass_assignment_sanitizer = :strict # print deprecation notices stderr config.active_support.deprecation = :stderr end
development.rb
rendezvous::application.configure # settings specified here take precedence on in config/application.rb # in development environment application's code reloaded on # every request. slows down response time perfect development # since don't have restart web server when make code changes. config.cache_classes = false # log error messages when accidentally call methods on nil. config.whiny_nils = true # show full error reports , disable caching config.consider_all_requests_local = true config.action_controller.perform_caching = false # don't care if mailer can't send config.action_mailer.raise_delivery_errors = false # print deprecation notices rails logger config.active_support.deprecation = :log # use best-standards-support built browsers config.action_dispatch.best_standards_support = :builtin # raise exception on mass assignment protection active record models config.active_record.mass_assignment_sanitizer = :strict # log query plan queries taking more (works # sqlite, mysql, , postgresql) config.active_record.auto_explain_threshold_in_seconds = 0.5 # not compress assets config.assets.compress = false # expands lines load assets config.assets.debug = true end
update 2
it seems errors happen when executing tests caused fixtures. if comment fixtures :all
loading on test_helper.rb
, create simple test asserts true, test pass, doesn't when fixtures loaded.
finally found reason why tests failed error. referencing constant declared in availability
concern using availabiliy::
, instead of using business::
. also, tests not in sync current code, made finding bug lot more difficult should. replacing references availability
business
solved problem. lesson here is: keep application , testing code in sync.
thank lichtamberg kind support.
Comments
Post a Comment