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

Ticket #10536: call_setup_method_in_subclassed_test_case.patch

File call_setup_method_in_subclassed_test_case.patch, 2.7 kB (added by kasatani, 10 months ago)

A patch which makes setup methods in subclassed test cases called.

  • activerecord/test/fixtures_test.rb

    old new  
    331331    fixtures :topics 
    332332 
    333333    def setup 
     334      @setup_done = true 
    334335    end 
    335336   
    336337    def test_should_properly_setup_fixtures 
     338      assert @setup_done 
    337339      assert_nothing_raised { topics(:first) } 
    338340    end 
    339341  end 
    340342CODE 
    3413432.times { eval(class_def) } 
    342344 
     345class BaseOfSubclassedTestCase < ActiveSupport::TestCase 
     346  def setup 
     347    @base_setup_done = true 
     348  end 
     349 
     350  def teardown 
     351    assert @subclass_teardown_done 
     352    @base_teardown_done = true 
     353  end 
     354end 
     355class SubclassedTestCase < BaseOfSubclassedTestCase 
     356  fixtures :topics 
     357 
     358  def setup 
     359    super 
     360    @subclass_setup_done = true 
     361  end 
     362   
     363  def teardown 
     364    @subclass_teardown_done = true 
     365    super 
     366    assert @base_teardown_done 
     367  end 
     368   
     369  def test_should_properly_call_all_setup_and_teardown_methods 
     370    assert @base_setup_done 
     371    assert @subclass_setup_done 
     372    assert_nothing_raised { topics(:first) } 
     373  end 
     374end 
     375 
    343376class OverlappingFixturesTest < Test::Unit::TestCase 
    344377  fixtures :topics, :developers 
    345378  fixtures :developers, :accounts 
  • activerecord/lib/active_record/fixtures.rb

    old new  
    972972         
    973973        case method.to_s 
    974974        when 'setup' 
    975           unless method_defined?(:setup_without_fixtures) 
    976             alias_method :setup_without_fixtures, :setup 
    977             define_method(:full_setup) do 
    978               setup_with_fixtures 
    979               setup_without_fixtures 
    980             end 
    981           end 
    982           alias_method :setup, :full_setup 
     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" 
    983978        when 'teardown' 
    984           unless method_defined?(:teardown_without_fixtures) 
    985             alias_method :teardown_without_fixtures, :teardown 
    986             define_method(:full_teardown) do 
    987               teardown_without_fixtures 
    988               teardown_with_fixtures 
    989             end 
    990           end 
    991           alias_method :teardown, :full_teardown 
     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" 
    992982        end 
    993983         
    994984        @__disable_method_added__ = false