ruby - How can I test logger-messages with MiniTest? -
i have application , want test if correct messages logger.
a short example (you may switch between log4r , logger):
gem 'minitest' require 'minitest/autorun' require 'log4r' #~ require 'logger' class testlog < minitest::test def setup if defined? log4r @log = log4r::logger.new('log') @log.outputters << log4r::stdoutoutputter.new('stdout', :level => log4r::info) else @log = logger.new(stdout) @log.level = logger::info end end def test_silent assert_silent{ @log.debug("hello world") } assert_output(nil,nil){ @log.debug("hello world") } end def test_output #~ refute_silent{ @log.info("hello") }#-> nomethoderror: undefined method `refute_silent' assert_output("info log: hello world\n",''){ @log.info("hello world") } end end
but get:
1) failure: testlog#test_output [minitest_log4r.rb:27]: in stdout. expected: "info log: hello world\n" actual: ""
on output screen see message. have similar results log4r::stderroutputter
, log4r::outputter.stdout
.
so seems send output screen, not catched minitest in stdout or stderr.
before start write minitest-log4r-gem:
is there possibility test logger-output in minitest?
if not: recommendations how implement minitest-log4r-gem?
examples imagine:
- define new outputter minitest (log4r::minitestoutputter)
- mock logger.
- new assertions (add new outputter parameter?):
assert_message('info log: hello world'){ @log.info("hello world") }
assert_messages(:info => 1, :debug => 2){ @log.info("hello world") }
count messages.assert_info_messages('hello world'){ @log.info("hello world") }
assert_debug_messages('hello world'){ @log.info("hello world") }
you can set pipe, pass writer pipe logger, , use reader pipe test assertions.
http://ruby-doc.org/core-2.1.0/io.html#method-c-pipe
something like:
require 'logger' r, w = io.pipe log = logger.new(w) log.info "testing info log message" output = w.gets puts "test passed: #{!!(/testing/ =~ output)}"
Comments
Post a Comment