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

Ticket #10379: make_subclassed_testcase_and_testcase_loaded_multiple_times_both_work.patch

File make_subclassed_testcase_and_testcase_loaded_multiple_times_both_work.patch, 2.9 kB (added by kasatani, 7 months ago)
  • activerecord/test/fixtures_test.rb

    old new  
    348348end 
    349349 
    350350 
     351# This is to reproduce a bug where if a TestCase is loaded 
     352# twice by Ruby, it loses its fixture setup hook. 
     353class_def = <<-CODE 
     354  class DoubleLoadedTestCase < Test::Unit::TestCase 
     355    fixtures :topics 
     356 
     357    def setup 
     358    end 
     359   
     360    def test_should_properly_setup_fixtures 
     361      assert_nothing_raised { topics(:first) } 
     362    end 
     363  end 
     364CODE 
     3652.times { eval(class_def) } 
     366 
    351367class OverlappingFixturesTest < Test::Unit::TestCase 
    352368  fixtures :topics, :developers 
    353369  fixtures :developers, :accounts 
  • activerecord/lib/active_record/fixtures.rb

    old new  
    916916      end 
    917917 
    918918      def setup_with_fixtures 
     919        return if @fixtures_setup 
     920        @fixtures_setup = true 
    919921        return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? 
    920922 
    921923        if pre_loaded_fixtures && !use_transactional_fixtures 
     
    947949      alias_method :setup, :setup_with_fixtures 
    948950 
    949951      def teardown_with_fixtures 
     952        return if @fixtures_teardown 
     953        @fixtures_teardown = true 
    950954        return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? 
    951955 
    952956        unless use_transactional_fixtures? 
     
    963967      alias_method :teardown, :teardown_with_fixtures 
    964968 
    965969      def self.method_added(method) 
     970        return if @__disable_method_added__ 
     971        @__disable_method_added__ = true 
     972         
    966973        case method.to_s 
    967974        when 'setup' 
    968           unless method_defined?(:setup_without_fixtures) 
    969             alias_method :setup_without_fixtures, :setup 
    970             define_method(:setup) do 
    971               setup_with_fixtures 
    972               setup_without_fixtures 
    973             end 
    974           end 
     975          original_setup = "setup_without_fixtures_for_#{self.name.underscore.gsub('/', '__')}" 
     976          alias_method original_setup, :setup 
     977          class_eval "def setup; setup_with_fixtures; #{original_setup}; end" 
    975978        when 'teardown' 
    976           unless method_defined?(:teardown_without_fixtures) 
    977             alias_method :teardown_without_fixtures, :teardown 
    978             define_method(:teardown) do 
    979               teardown_without_fixtures 
    980               teardown_with_fixtures 
    981             end 
    982           end 
     979          original_teardown = "teardown_without_fixtures_for_#{self.name.underscore.gsub('/', '__')}" 
     980          alias_method original_teardown, :teardown 
     981          class_eval "def teardown; #{original_teardown}; teardown_with_fixtures; end" 
    983982        end 
     983         
     984        @__disable_method_added__ = false 
    984985      end 
    985986 
    986987      private