title: How can I clear class variables between rspec tests in ruby
body: <p>I have the following class:
  I want to ensure the class url is only set once for all instances.</p>
  
  <pre><code>class DataFactory
    @@url = nil
  
    def initialize()
  begin
      if @@url.nil?
         Rails.logger.debug "Setting url"
         @@url = MY_CONFIG["my value"]
      end
  rescue Exception
    raise DataFactoryError, "Error!"
  end
    end
  end
  </code></pre>
  
  <p>I have two tests:</p>
  
  <pre><code>it "should log a message" do
    APP_CONFIG = {"my value" =&gt; "test"}
    Rails.stub(:logger).and_return(logger_mock)
    logger_mock.should_receive(:debug).with "Setting url"
  
    t = DataFactory.new
    t = nil
  end
  
  it "should throw an exception" do
    APP_CONFIG = nil
  
    expect {
      DataFactory.new
    }.to raise_error(DataFactoryError, /Error!/)
  end
  </code></pre>
  
  <p>The problem is the second test never throws an exception as the @@url class variable is still set from the first test when the second test runs.
  Even though I have se the instance to nil at the end of the first test garbage collection has not cleared the memory before the second test runs:</p>
  
  <p>Any ideas would be great!
  I did hear you could possibly use Class.new but I am not sure how to go about this.</p>