title: Can I stub STDERR in RSpec?
body: <p>I have a simple function that I'd like to test (perhaps mostly to appease simplecov).  The function is:</p>
  
  <pre><code>module Utils
    extend self
  
    def blather(msg)
      msg = "=== " + msg
      STDERR.puts(msg)
      Rails.logger.debug(msg)
    end
  
  end
  </code></pre>
  
  <p>The <a href="https://www.relishapp.com/rspec/rspec-mocks/docs/method-stubs/stub-on-any-instance-of-a-class" rel="nofollow">RSpec documentation for stubbing</a> says that:</p>
  
  <blockquote>
    <p>Messages can be stubbed on any class, including those in Ruby's core library.</p>
  </blockquote>
  
  <p>But the following:</p>
  
  <pre><code># file: spec/lib/utils_spec.rb
  require 'spec_helper'
  describe Utils do
    context "blather" do
      it "should print to STDERR" do
        STDERR.any_instance.should_receive(:puts).with("=== zoo")    
        Utils.blather("zoo")
      end
    end
  end
  </code></pre>
  
  <p>... I get an error</p>
  
  <pre><code>undefined method `any_instance' for #&lt;IO:&lt;STDERR&gt;&gt;
  </code></pre>
  
  <p>Putting aside questions as to whether this test makes any sense, is it possible to stub STDERR (the IO class)?  Is this failing because it's a class method?  Or is there a more sensible strategy for this kind of test?</p>