Last Updated: August 04, 2018
·
1.121K
· coconup

Ruby on Rails: test logs output presence within Rails.logger (through Minitest too)

require 'test_helper'

class MyTest < ActionDispatch::IntegrationTest
  def with_logger_introspection(&block)
    orig_logger = Rails.logger.dup
    @logger_output = StringIO.new
    begin
      Rails.logger = ActiveSupport::Logger.new(@logger_output)
      block.call(@logger_output)
    ensure
      Rails.logger = orig_logger
    end
  end

  test "Something is logged" do
    with_logger_introspection do |logger_output|
      # do something
      assert logger_output.string.include? "Something"
    end
  end
end