Create a gist now

Instantly share code, notes, and snippets.

Embed
Silence RSpec specs
RSpec.configure do |config|
config.before(:all, &:silence_output)
config.after(:all, &:enable_output)
end
# Redirects stderr and stdout to /dev/null.
def silence_output
@orig_stderr = $stderr
@orig_stdout = $stdout
# redirect stderr and stdout to /dev/null
$stderr = File.new('/dev/null', 'w')
$stdout = File.new('/dev/null', 'w')
end
# Replace stdout and stderr so anything else is output correctly.
def enable_output
$stderr = @orig_stderr
$stdout = @orig_stdout
@orig_stderr = nil
@orig_stdout = nil
end
@maciejsmolinski

This comment has been minimized.

Show comment
Hide comment
@maciejsmolinski

maciejsmolinski Aug 17, 2011

This gist helped me a lot and saved a lot of time :) Thank you Adam !

This gist helped me a lot and saved a lot of time :) Thank you Adam !

@adamstegman

This comment has been minimized.

Show comment
Hide comment
@adamstegman

adamstegman Aug 18, 2011

Credit where it's due - this is adapted from http://benevolentcode.com/2011/03/temporarily-redirect-stdout-in-ruby/. Glad you found it useful!

Owner

adamstegman commented Aug 18, 2011

Credit where it's due - this is adapted from http://benevolentcode.com/2011/03/temporarily-redirect-stdout-in-ruby/. Glad you found it useful!

@jimjh

This comment has been minimized.

Show comment
Hide comment
@jimjh

jimjh Dec 26, 2012

Thanks for the gist!

I am getting some errors regarding "private method `silence_output' called for #RSpec::Core::ExampleGroup::Nested_1:0x007f7f8aa89b00". Fixed by changing to the following:

config.before(:all) { silence_output }
config.after(:all) { enable_output }

jimjh commented Dec 26, 2012

Thanks for the gist!

I am getting some errors regarding "private method `silence_output' called for #RSpec::Core::ExampleGroup::Nested_1:0x007f7f8aa89b00". Fixed by changing to the following:

config.before(:all) { silence_output }
config.after(:all) { enable_output }
@mikejarema

This comment has been minimized.

Show comment
Hide comment
@mikejarema

mikejarema Jan 4, 2013

I encountered the same errors as @jimjh when using this gist and can confirm the fix works with Rails 3.2.8 & Rspec 2.12.0, thanks!

I encountered the same errors as @jimjh when using this gist and can confirm the fix works with Rails 3.2.8 & Rspec 2.12.0, thanks!

@Paxa

This comment has been minimized.

Show comment
Hide comment
@Paxa

Paxa Jan 12, 2015

Also can use Kernel.silence_stream:

silence_stream(STDOUT) do
 ...
end

Paxa commented Jan 12, 2015

Also can use Kernel.silence_stream:

silence_stream(STDOUT) do
 ...
end
@AdilKhn

This comment has been minimized.

Show comment
Hide comment
@AdilKhn

AdilKhn Feb 27, 2015

Great post. I modified silence_output to redirect the output to files instead of the void. Works out great.

def silence_output
  @orig_stderr = $stderr
  @orig_stdout = $stdout

  # redirect stderr and stdout to /dev/null
  $stderr = File.new('./spec/stdout.txt', 'w')
  $stdout = File.new('./spec/stderr.txt', 'w')
end

AdilKhn commented Feb 27, 2015

Great post. I modified silence_output to redirect the output to files instead of the void. Works out great.

def silence_output
  @orig_stderr = $stderr
  @orig_stdout = $stdout

  # redirect stderr and stdout to /dev/null
  $stderr = File.new('./spec/stdout.txt', 'w')
  $stdout = File.new('./spec/stderr.txt', 'w')
end
@marshal003

This comment has been minimized.

Show comment
Hide comment
@marshal003

marshal003 May 19, 2016

Hi guys, above mentioned methods doesn't help me in redirecting stdout for system call (` method). But thanks you all, these examples helped me in identifying the workaround. Here is the workaround I have used.

config.before :each do
    original_system_call_method = GitUtils.method(:`)
    allow(GitUtils).to receive(:`) do |arg|
      Kernel.silence_stream(STDERR) do
        original_system_call_method.call(arg)
      end
    end
  end

# GitUtils is my class where I have multiple utility methods which usage backtick(`) to run shell commands

Hi guys, above mentioned methods doesn't help me in redirecting stdout for system call (` method). But thanks you all, these examples helped me in identifying the workaround. Here is the workaround I have used.

config.before :each do
    original_system_call_method = GitUtils.method(:`)
    allow(GitUtils).to receive(:`) do |arg|
      Kernel.silence_stream(STDERR) do
        original_system_call_method.call(arg)
      end
    end
  end

# GitUtils is my class where I have multiple utility methods which usage backtick(`) to run shell commands
@wzcolon

This comment has been minimized.

Show comment
Hide comment
@wzcolon

wzcolon Jul 13, 2016

Any way to still use a debugger?

wzcolon commented Jul 13, 2016

Any way to still use a debugger?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment