Getting uninitialized constant error when trying to run tests

后端 未结 12 2220
礼貌的吻别
礼貌的吻别 2020-12-10 13:29

I just updated all my gems and I\'m finding that I\'m getting errors when trying to run Test::Unit tests. I\'m getting the error copied below. That comes from creating new,

相关标签:
12条回答
  • 2020-12-10 13:32

    This can happen if modules are declared in a single statement when the parent module they are nested inside has not yet been loaded. I haven't looked at the code in those gems, but my hunch is that's what is happening. Chuck's solution would suggest that. calling gem 'test-unit' first will load the parent module, so the setup of zen test ends up working ok.

    e.g.

    module Foo::Bar
      def do_stuff
        # insert awesomeness here...
      end
    end
    

    Will result in an error if the parent Foo module hasn't already been defined (e.g. by another gem)

    A safer way to declare this is

    module Foo
      module Bar
        def do_stuff
          # insert awesomeness here...
        end
      end
    end
    

    May be a bug in Zentest that needs patching.

    0 讨论(0)
  • 2020-12-10 13:33

    Same issue with me too. Nothing above mentioned worked for me except downgrading test-unit back to 1.2.3 I'm missing the coloring of the test-unit 2.x

    0 讨论(0)
  • 2020-12-10 13:35

    I ran into this today on Mac OS X 10.6. My solution is as follows:

    config.gem 'test-unit', :lib => 'test/unit', :version => '1.2.3'
    config.gem 'autotest'
    config.gem 'cucumber'
    config.gem 'cucumber-rails', :lib => false
    config.gem 'ffaker', :lib => 'faker'
    config.gem 'rspec', :lib => false, :version => '>= 1.2.0'
    config.gem 'rspec-rails', :lib => false, :version => '>= 1.2.0'
    config.gem 'selenium-client', :lib => 'selenium'
    config.gem "thoughtbot-factory_girl", :lib => 'factory_girl', :source => "http://gems.github.com"
    config.gem 'thoughtbot-shoulda', :lib => 'shoulda'
    config.gem 'webrat'
    config.gem 'ZenTest', :lib => 'zentest'
    
    0 讨论(0)
  • 2020-12-10 13:37

    I am not a nuby to rails but I am still learning and hopefully always will be :-).

    Rails 2.3 production environment using Ruby Enterprise Edition and passenger can produce a totally misleading useless error during startup (/var/log/passenger.log). Something like:

    Exception NameError in PhusionPassenger::Rack::ApplicationSpawner (uninitialized constant XXX)

    If you run script/console on the production server, you may see:

    Loading production environment (Rails 2.3.4)
    /home/ubuntu/MyApp/shared/bundle/ruby/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:105:in  const_missing:NameError: uninitialized constant XXX
    

    This is happening to me in this instance only in production environment, not staging, not development. After a full day of research and experiments, I have concluded that in the production environment, REE or something must pre-load classes and the pre-loader apparently does not like to see classes being re-opened before they are created (an educated guess).

    In app/models/calendar.rb:

      Class Calendar < ActiveRecord::Base
        # This defines the class
      End
    

    In app/models/event.rb

      Class Event < ActiveRecord::Base
        # This desined the class
      End
      Class Calendar
        # This is supposed to 're-open' the Calendar class
        has_many :events
      end
    

    The above generic sample code snippet can cause startup issue. I am not sure of the order that pre-loading classes takes place but I suspect that may be the issue. I moved the ‘has_many :events’ into the class definition in app/modeles/calendar.rb and my app now starts without error.

    So, a good rule to follow is to put your Active Record Associations (has_many, belongs_to) inside the defining class (where the class is created).

    0 讨论(0)
  • 2020-12-10 13:40

    As this link suggests http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/50 it may happen due to preliminary initialization of mocha lib. To prevent it from happeing it is advisable to add line

    config.gem 'test-unit', :lib => 'test/unit'
    

    to config/environment.rb

    0 讨论(0)
  • 2020-12-10 13:40

    Here is the recipe for test_unit 2.0.7 on Rails 2.3.5:

    In config/environments/test.rb:

    config.gem 'test-unit', :version => '2.0.7', :lib => false
    

    In test_helper.rb, add require 'test/unit', immediately after require 'test_help'

    So it looks like this:

    require 'test_help'
    require 'test/unit'
    

    If you receive this error: %': one hash required (ArgumentError), upgrade gem i18n to v0.3.6.

    0 讨论(0)
提交回复
热议问题