Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean solution for resetting class variables in between rspec tests

Is there a better way to clean class variables in between Rspec tests than to clear them explicitely from the after(:each) method? I would prefer a way that "clears them all", instead of having to remember adding them to the config.after every time again...

config.after(:each) do
  DatabaseCleaner.clean

  Currency.class_variable_set :@@default, nil
end
like image 385
Danny Avatar asked Sep 29 '13 08:09

Danny


3 Answers

I finally implemented this by defining a method in lib/auto_clean_class_variables.rb to reset all class variables of a given 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 within 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

The models that need "cleaning" can bootstrap this behaviour:

extend AutoCleanClassVariables
CLASS_VARIABLES_TO_CLEAN = [:@@default]

This way everything works fine, and there is no (unnecessary) reload of the class leading to problems if you try to compare objects of these classes with each other (see my earlier comment)

like image 90
Danny Avatar answered Nov 12 '22 22:11

Danny


You should be able to reload the class after every run:

after(:each) do
  Object.send(:remove_const, 'Currency')
  load 'currency.rb'
end

See: RSpec Class Variable Testing

like image 37
Chris Salzberg Avatar answered Nov 12 '22 22:11

Chris Salzberg


include this in your spec_helper.rb

Spec::Runner.configure do |config|
  config.before(:all) {}
  config.before(:each) {
       Class.class_variable_set :@@variable, value
  }
  config.after(:all) {}
  config.after(:each) {}
end

This will run what you choose before every different test. The accepted solution did not solve at least 2 cases I had where I needed a class variable reset.

like image 1
Christopher Reid Avatar answered Nov 12 '22 23:11

Christopher Reid