How to reload ruby ​​class

In many of our classes, we cache expensive work for performance. eg.

def self.foo
    @foo ||= get_foo
end 

This works fine in the application, however tests (RSpec) fail due to these memoized variables. Values ​​from the first test are returned in subsequent tests when we expect fresh values.

So the question is: how do I reload the class? Or delete all noticed variables?

+5
source share
2 answers

Build your classes and tests so that the cached data remains valid or is deleted when it is invalid. Think about how to add a cache flush method and call it in the rspec block before.

+3
source

after ( before) , (, ):

after do
  subject.instance_variable_set(:@foo, nil)
end

. memoized , . , .

+7

All Articles