Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11056: load_errors_should_not_be_silent.diff

File load_errors_should_not_be_silent.diff, 2.1 kB (added by stuthulhu, 8 months ago)
  • test/cases/fixtures_test.rb

    old new  
    590590    assert_equal parrots(:louis), Parrot.find_by_name("King Louis") 
    591591  end 
    592592end 
     593 
     594class FixtureLoadingTest < ActiveRecord::TestCase 
     595  def test_logs_message_for_failed_dependency_load 
     596    Test::Unit::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError) 
     597    ActiveRecord::Base.logger.expects(:warn) 
     598    Test::Unit::TestCase.try_to_load_dependency(:does_not_exist) 
     599  end 
     600 
     601  def test_does_not_logs_message_for_successful_dependency_load 
     602    Test::Unit::TestCase.expects(:require_dependency).with(:works_out_fine) 
     603    ActiveRecord::Base.logger.expects(:warn).never 
     604    Test::Unit::TestCase.try_to_load_dependency(:works_out_fine) 
     605  end 
     606end 
  • lib/active_record/fixtures.rb

    old new  
    846846          setup_fixture_accessors(table_names) 
    847847        end 
    848848 
     849        def try_to_load_dependency(file_name) 
     850          require_dependency file_name 
     851        rescue LoadError => e 
     852          # Let's hope the developer has included it himself 
     853           
     854          # Let's warn in case this is a subdependency, otherwise 
     855          # subdependency error messages are totally cryptic 
     856          ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}") 
     857        end 
     858         
    849859        def require_fixture_classes(table_names = nil) 
    850860          (table_names || fixture_table_names).each do |table_name| 
    851861            file_name = table_name.to_s 
    852862            file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names 
    853             begin 
    854               require_dependency file_name 
    855             rescue LoadError 
    856               # Let's hope the developer has included it himself 
    857             end 
     863            try_to_load_dependency(file_name) 
    858864          end 
    859865        end 
    860866