Skip to content
This repository
Browse code

Make the Fixtures Test::Unit enhancements more supporting for double-…

…loaded test cases. Closes #10379 [brynary]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8392 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit abd7cf343c3f43516d25e3b787e96af38d12ebf4 1 parent 1ceccde
risk danger olson authored December 15, 2007
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Make the Fixtures Test::Unit enhancements more supporting for double-loaded test cases.  Closes #10379 [brynary]
  4
+
3 5
 * Fix that validates_acceptance_of still works for non-existent tables (useful for bootstrapping new databases).  Closes #10474 [hasmanyjosh]
4 6
 
5 7
 * Ensure that the :uniq option for has_many :through associations retains the order.  #10463 [remvee]
15  activerecord/lib/active_record/fixtures.rb
@@ -916,6 +916,8 @@ def use_transactional_fixtures?
916 916
       end
917 917
 
918 918
       def setup_with_fixtures
  919
+        return if @fixtures_setup
  920
+        @fixtures_setup = true
919 921
         return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?
920 922
 
921 923
         if pre_loaded_fixtures && !use_transactional_fixtures
@@ -947,6 +949,8 @@ def setup_with_fixtures
947 949
       alias_method :setup, :setup_with_fixtures
948 950
 
949 951
       def teardown_with_fixtures
  952
+        return if @fixtures_teardown
  953
+        @fixtures_teardown = true
950 954
         return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?
951 955
 
952 956
         unless use_transactional_fixtures?
@@ -963,24 +967,31 @@ def teardown_with_fixtures
963 967
       alias_method :teardown, :teardown_with_fixtures
964 968
 
965 969
       def self.method_added(method)
  970
+        return if @__disable_method_added__
  971
+        @__disable_method_added__ = true
  972
+        
966 973
         case method.to_s
967 974
         when 'setup'
968 975
           unless method_defined?(:setup_without_fixtures)
969 976
             alias_method :setup_without_fixtures, :setup
970  
-            define_method(:setup) do
  977
+            define_method(:full_setup) do
971 978
               setup_with_fixtures
972 979
               setup_without_fixtures
973 980
             end
974 981
           end
  982
+          alias_method :setup, :full_setup
975 983
         when 'teardown'
976 984
           unless method_defined?(:teardown_without_fixtures)
977 985
             alias_method :teardown_without_fixtures, :teardown
978  
-            define_method(:teardown) do
  986
+            define_method(:full_teardown) do
979 987
               teardown_without_fixtures
980 988
               teardown_with_fixtures
981 989
             end
982 990
           end
  991
+          alias_method :teardown, :full_teardown
983 992
         end
  993
+        
  994
+        @__disable_method_added__ = false
984 995
       end
985 996
 
986 997
       private
16  activerecord/test/fixtures_test.rb
@@ -324,6 +324,22 @@ def test_fixture_table_names
324 324
   end
325 325
 end
326 326
 
  327
+# This is to reproduce a bug where if a TestCase is loaded
  328
+# twice by Ruby, it loses its fixture setup hook.
  329
+class_def = <<-CODE
  330
+  class DoubleLoadedTestCase < Test::Unit::TestCase
  331
+    fixtures :topics
  332
+
  333
+    def setup
  334
+    end
  335
+  
  336
+    def test_should_properly_setup_fixtures
  337
+      assert_nothing_raised { topics(:first) }
  338
+    end
  339
+  end
  340
+CODE
  341
+2.times { eval(class_def) }
  342
+
327 343
 class OverlappingFixturesTest < Test::Unit::TestCase
328 344
   fixtures :topics, :developers
329 345
   fixtures :developers, :accounts

0 notes on commit abd7cf3

Please sign in to comment.
Something went wrong with that request. Please try again.