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.

+3


source to share


2 answers


Found a less accurate but more convenient reading way:

require 'rspec'

describe 'My behaviour' do

  it 'should do something' do
    expect { system('echo "foo" 1>&2 ') }.to output.to_stderr_from_any_process
  end
end

      



AFAIU - it can't verify the exact message, but that's enough for me

0


source


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

      

+2


source







All Articles