Danny
Danny

Reputation: 6025

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

Upvotes: 8

Views: 6880

Answers (3)

Danny
Danny

Reputation: 6025

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)

Upvotes: 6

Christopher Reid
Christopher Reid

Reputation: 4482

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.

Upvotes: 1

Chris Salzberg
Chris Salzberg

Reputation: 27374

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

Upvotes: 4

Related Questions