title: Isolating meta-programming specs that fail intermittantly (open classes / monkey patching)
body: <p>I am trying to refactor a spec that currently passes or fails based off the order in which the examples are run.</p>
  
  <p>Spec is below.</p>
  
  <p>If the first example runs first, the spec passes. </p>
  
  <p>If the second test runs before the first, it fails. I understand why it fails -- there are three subclasses of <code>FieldExtractor</code> in ObjectSpace so the assertion <code>expect(FieldExtractor.all.size).to eq 2</code> will be false. </p>
  
  <pre><code>RSpec.describe FieldExtractor do
  
    it 'keeps track of subclasses' do
      class TotalExtractor &lt; FieldExtractor
      end
      class VendorExtractor &lt; FieldExtractor
      end
  
      expect(FieldExtractor.all).to include(TotalExtractor)
      expect(FieldExtractor.all).to include(VendorExtractor)
      expect(FieldExtractor.all.size).to eq 2
    end
  
    it 'keeps track of extracted field' do
      class ConfiguredExtractor &lt; FieldExtractor
        extracts :vendor
      end
  
      expect(ConfiguredExtractor.field).to eq :vendor
    end
  end
  </code></pre>
  
  <p>My question is how to fix this intermittant test failure.</p>
  
  <p>Is there an elegant way around this? Is the way I'm testing this totally off-base?</p>
  
  <p>One solution I got working but don't particularly care for is defining all the subclasses at the beginning of the spec (outside the examples) so that I know exactly how many there should be. I don't like this solution because I have many examples with many subclasses and it doesn't seem to scale well.</p>