Skip to content

Instantly share code, notes, and snippets.

Created April 19, 2011 05:28
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Silence RSpec specs
RSpec.configure do |config|
config.before(:all, &:silence_output)
config.after(:all, &:enable_output)
# Redirects stderr and stdout to /dev/null.
def silence_output
@orig_stderr = $stderr
@orig_stdout = $stdout
# redirect stderr and stdout to /dev/null
$stderr ='/dev/null', 'w')
$stdout ='/dev/null', 'w')
# 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
Copy link

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

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

Copy link

wzcolon commented Jul 13, 2016

Any way to still use a debugger?

Copy link

this also works:

bin/rspec spec/models/user_spec.rb 2>/dev/null

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