Rspec - stderr output of linux process
I want to check that my bash script is outputting some kind of message to stderr. I am trying this:
require 'rspec'
describe 'My behaviour' do
it 'should do something' do
expect { `echo "foo" 1>&2` }.to output.to_stderr
end
end
But it seems that the output to stderr did not happen during the test.
source to share
RSpec output.to_stderr
matcher looks for things that write $stdout
/ $stderr
- which your shell command doesn't execute as it runs as a separate subprocess.
To test this, you need to explicitly commit stdout
and stderr
shell code. You can easily create your own implementation using the standard Open3
standard library, or use rspec-bash
gem for example :
require 'rspec'
require 'rspec/bash'
describe 'My behaviour' do
include Rspec::Bash
let(:stubbed_env) { create_stubbed_env }
it 'should do something' do
stdout, stderr, status = stubbed_env.execute(
'echo "foo" 1>&2'
)
expect(stderr).to eq('foo')
end
end
source to share