RSpec: Quiet a socketry/console logger in a test

You are writing a test.

It raises a well-known exception, and you check for it.

Suddenly, there’s log output, since you are working with some concurrent task. Your mood sinks. What’s the way to disable this library’s logging? In this case, I was kindly helped by the library’s author.

Quiet a Console.logger in an RSpec test. Use an around hook:

around do |example|
  level = Console.logger.level
  Console.logger.off!

  example.run

  Console.logger.level = level
end

First, store the existing level, in order to restore it after the test (aka example) has run.

Then, disable the logger and run the example.

Finally, restore the logger’s previous level.

The Console gem is amazing, and comes bundled with the Async gem. Well thought-out gems.

Published by olleolleolle

Olle is a programmer, enjoying sunny Malmö in Sweden.

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.