title: RSpec: how to test Rails logger message expectations?
body: <p>I am trying to test that the Rails logger receives messages in some of my specs.  I am using the <a href="https://github.com/TwP/logging" rel="noreferrer">Logging gem</a>.</p>
  
  <p>Let's say that I have a class like this:</p>
  
  <pre><code>class BaseWorker
  
    def execute
      logger.info 'Starting the worker...'
    end
  
  end
  </code></pre>
  
  <p>And a spec like:</p>
  
  <pre><code>describe BaseWorker do
  
    it 'should log an info message' do
      base_worker = BaseWorker.new
      logger_mock = double('Logging::Rails').as_null_object
      Logging::Rails.stub_chain(:logger, :info).and_return(logger_mock)
  
      logger_mock.should_receive(:info).with('Starting the worker...')
      base_worker.execute
      Logging::Rails.unstub(:logger)
    end
  
  end
  </code></pre>
  
  <p>I get the following failure message:</p>
  
  <pre><code> Failure/Error: logger_mock.should_receive(:info).with('Starting worker...')
     (Double "Logging::Rails").info("Starting worker...")
         expected: 1 time
         received: 0 times
  </code></pre>
  
  <p>I've tried out several different approaches to get the spec to pass.  This works for example:</p>
  
  <pre><code>class BaseWorker
  
    attr_accessor :log
  
    def initialize
      @log = logger
    end
  
    def execute
      @log.info 'Starting the worker...'
    end
  
  end
  
  describe BaseWorker do
    it 'should log an info message' do
      base_worker = BaseWorker.new
      logger_mock = double('logger')
      base_worker.log = logger_mock
  
      logger_mock.should_receive(:info).with('Starting the worker...')
      base_worker.execute
    end
  end
  </code></pre>
  
  <p>But having to setup an accessible instance variable like that seems like the tail is wagging the dog here. (Actually, I'm not even sure why copying logger to @log would make it pass.)</p>
  
  <p>What's a good solution for testing the logging?</p>