'Suppress console output during RSpec tests

I am testing the class which put on the console some messages (with puts, p warnings and etc.). I am just wondering if there is any ability to suppress this output during RSpec tests ?



Solution 1:[1]

Try stubbing methods that make the output in a before block, e.g.

before do
  IO.any_instance.stub(:puts) # globally
  YourClass.any_instance.stub(:puts) # or for just one class
end

This is explicit, so you won't miss anything you don't want to miss. If you don't care about any output and the method above doesn't work you can always stub the IO object itself:

before do
  $stdout.stub(:write) # and/or $stderr if needed
end

Solution 2:[2]

An Rspec3.0 Version would be => in spec_helper.rb

RSpec.configure do |c|
  c.before { allow($stdout).to receive(:puts) }
end

it will act as before(:each)

but :each is default, so no need to write it explicitly

Solution 3:[3]

Tested with rspec-core (~> 3.4.0)

In describe block you could do

# spec_helper.rb
def suppress_log_output
  allow(STDOUT).to receive(:puts) # this disables puts
  logger = double('Logger').as_null_object
  allow(Logger).to receive(:new).and_return(logger)
end

# some_class_spec.rb
RSpec.describe SomeClass do
  before do
    suppress_log_output
  end
end

This way you have the advantage of toggling log output for specific tests. Note, this will not suppress rspec warnings, or messages from rspec.

Another way to disable warnings coming from gems:

add config.warnings = false to spec_helper

If you wanted to suppress only certain logger methods, like error, info, or warn you could do

allow_any_instance_of(Logger).to receive(:warn).and_return(nil)

To disable warnings coming from the rspec gem

allow(RSpec::Support).to receive(:warning_notifier).and_return(nil)

but this is generally discouraged because it is meant as a way to let you know you are doing something smelly in your tests.

Solution 4:[4]

If you want to suppress output for a single test, there is a more concise way:

it "should do something with printing" do
  silence_stream(STDOUT) do
    foo.print.should be_true
  end
end

You may want to change STDOUT to STDERR if your test prints an error.

Solution 5:[5]

Updated answer for Rails 5, in a one-off situation:

before do
  RSpec::Mocks.with_temporary_scope do
    allow(STDOUT).to receive(:puts)
  end
end

You can make this into a method in spec_helper if you'll be doing this a lot.

Solution 6:[6]

After trying all of these examples, I ended up using this varation which does not silence or mute binding.pry

# frozen_string_literal: true

RSpec.configure do |config|
  config.before(:each) do
    allow($stdout).to receive(:puts)
    allow($stdout).to receive(:write)
  end
end

Solution 7:[7]

It can be useful to inject an IO object defaulting to STDOUT. This also makes it easier to assert on the output if you want to.

E.g.

def my_method(arg, io: STDOUT)
  io.puts "hello"
  arg.reverse
end

And then in your test:

# Suppress it.

my_method("hi", io: StringIO.new)

# Assert on it.

io = StringIO.new
my_method("hi", io: io)

output = io.tap(&:rewind).read
expect(output).to include("hello")

Solution 8:[8]

You could have the object itself supress based on the environment:

class Foo
  def call
    puts("blah blah")
    # ...
  end

  private
  
  def puts(msg)
    return if ENV['APP_ENV'] == 'test'
    super
  end
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michał Kwiatkowski
Solution 2 jaiks
Solution 3
Solution 4
Solution 5 Alex Harvey
Solution 6 Chris Hough
Solution 7 Henrik N
Solution 8 Kris