title: how to test ruby inheritance with rspec
body: <p>I am trying to test logic that runs during class inheritance, but have run into an issue when running multiple assertions.</p>
  
  <p>i first tried...</p>
  
  <pre><code>describe 'self.inherited' do
    before do
      class Foo
        def self.inherited klass; end
      end
  
      Foo.stub(:inherited)
  
      class Bar &lt; Foo; end
    end
  
    it 'should call self.inherited' do
      # this fails if it doesn't run first
      expect(Foo).to have_received(:inherited).with Bar
    end
  
    it 'should do something else' do
      expect(true).to eq true
    end
  end
  </code></pre>
  
  <p>but this fails because the Bar class has already been loaded, and therefore does not call <code>inherited</code> a 2nd time.  If the assertion doesn't run first... it fails.</p>
  
  <p>So then i tried something like...</p>
  
  <pre><code>describe 'self.inherited once' do
    before do
      class Foo
        def self.inherited klass; end
      end
  
      Foo.stub(:inherited)
  
      class Bar &lt; Foo; end
    end
  
    it 'should call self.inherited' do
      @tested ||= false
      unless @tested
        expect(Foo).to have_receive(:inherited).with Bar
        @tested = true
      end
    end
  
    it 'should do something else' do
      expect(true).to eq true
    end
  end
  </code></pre>
  
  <p>because <code>@tested</code> doesn't persist from test to test, the test doesn't just run once.</p>
  
  <p>anyone have any clever ways to accomplish this?  This is a contrived example and i dont <em>actually</em> need to test ruby itself ;)</p>