I finally implemented this by pointing the method in lib / auto_clean_class_variables.rb to reset all class variables of this class
module AutoCleanClassVariables def reset_class_variables self::CLASS_VARIABLES_TO_CLEAN.each do |var| class_variable_set var, nil end end end
and calling this method from config.after (: each) located in spec_helper.rb
config.append_after(:each) do DatabaseCleaner.clean # Find all ActiveRecord classes having :reset_class_variables defined classes = ActiveSupport::DescendantsTracker.descendants(ActiveRecord::Base) classes.each do |cl| if cl.methods.include?(:reset_class_variables) cl.reset_class_variables end end end
Models that need to be βcleanedβ can load this behavior:
extend AutoCleanClassVariables CLASS_VARIABLES_TO_CLEAN = [:@@default]
Thus, everything works fine, and there is no (unnecessary) reloading of the class, leading to problems if you try to compare the objects of these classes with each other (see my previous comment)
Danny source share