title: Rspec any_instance.stub raises undefined method `any_instance_recorder_for' for nil:NilClass exception
body: <p>Here is the class that I'm testing contained in <strong>Foo.rb</strong>:</p>
  
  <pre><code>class Foo
      def bar
          return 2
      end
  end
  </code></pre>
  
  <p>Here is the my test contained in <strong>Foo_spec.rb</strong>:</p>
  
  <pre><code>require "./Foo.rb"
  
  describe "Foo" do
      before(:all) do
          puts "#{Foo == nil}"
          Foo.any_instance.stub(:bar).and_return(1)
      end
  
      it "should pass this" do
          f = Foo.new
          f.bar.should eq 1
      end
  end
  </code></pre>
  
  <p>I am getting the following output:</p>
  
  <pre><code>false
  F
  
  Failures:
  
    1) Foo Should pass this
       Failure/Error: Foo.any_instance.stub(:bar).and_return(1)
       NoMethodError:
         undefined method `any_instance_recorder_for' for nil:NilClass
       # ./Foo_spec.rb:6:in `block (2 levels) in &lt;top (required)&gt;'
  
  Finished in 0 seconds
  1 example, 1 failure
  
  Failed examples:
  
  rspec ./Foo_spec.rb:9 # Foo Should pass this
  </code></pre>
  
  <p>I've consulted <a href="https://www.relishapp.com/rspec/rspec-mocks/v/2-6/docs/method-stubs/stub-on-any-instance-of-a-class" rel="noreferrer">the doc</a> and the example given is passing on my machine (so it isn't a problem with the rspec code), but it isn't giving me any information on what I might be doing wrong. The error message is also quite confusing as it's telling me not to call <code>.any_instance</code> on a <code>nil:NilClass</code>, but as I proved with my output, <code>Foo</code> isn't <code>nil</code>. How am I supposed to call <code>.any_instance.stub</code> on my custom object?</p>
  
  <p>I'm using Ruby <code>1.9.3</code> and rspec <code>2.14.5</code>.</p>