MiniTest #capture_io temporarily switches $stdout and $stderr for StringIO objects to write the output written to $stdout or $stderr . But Logger has its own link to the original standard error stream, which he will gladly write. I think you can consider this a mistake, or at least a limitation of MiniTest #capture_io .
In your case, you create a Logger inside the block before #capture_io with the argument STDERR . STDERR still points to the original standard error stream, so it does not work properly.
Changing STDERR to $stderr (which at these points points to a StringIO object) works around this problem, but only if Logger is actually created in the #capture_io block, because outside it block it indicates the original standard error stream.
class T def do_something log = Logger.new($stderr) log.info("Here is an info message") end end
source share