Method: RSpec::Matchers#output

Defined in:
lib/rspec/matchers.rb

#output(expected = nil) ⇒ Object Also known as: a_block_outputting

Note:

to_stdout and to_stderr work by temporarily replacing $stdout or $stderr, so they're not able to intercept stream output that explicitly uses STDOUT/STDERR or that uses a reference to $stdout/$stderr that was stored before the matcher was used.

Note:

to_stdout_from_any_process and to_stderr_from_any_process use Tempfiles, and are thus significantly (~30x) slower than to_stdout and to_stderr.

With no arg, passes if the block outputs to_stdout or to_stderr. With a string, passes if the block outputs that specific string to_stdout or to_stderr. With a regexp or matcher, passes if the block outputs a string to_stdout or to_stderr that matches.

To capture output from any spawned subprocess as well, use to_stdout_from_any_process or to_stderr_from_any_process. Output from any process that inherits the main process's corresponding standard stream will be captured.

Examples:

expect { print 'foo' }.to output.to_stdout
expect { print 'foo' }.to output('foo').to_stdout
expect { print 'foo' }.to output(/foo/).to_stdout

expect { do_something }.to_not output.to_stdout

expect { warn('foo') }.to output.to_stderr
expect { warn('foo') }.to output('foo').to_stderr
expect { warn('foo') }.to output(/foo/).to_stderr

expect { do_something }.to_not output.to_stderr

expect { system('echo foo') }.to output("foo\n").to_stdout_from_any_process
expect { system('echo foo', out: :err) }.to output("foo\n").to_stderr_from_any_process


752
753
754
# File 'lib/rspec/matchers.rb', line 752

def output(expected=nil)
  BuiltIn::Output.new(expected)
end