Stephen Grayson
Stephen Grayson

Reputation: 35

How to capture the value of an abort or exit with Rspec

I am trying to write an Rspec test for my script that will pass if my script fails gracefully.

if options[:file] == false
  abort 'Missing an argument'
end

Rspec:

it 'if the file argument is not given, it provides a helpful message' do
  expect(`ruby example_file.rb`).to eq('Missing an argument')
end

My test continues to fail and says

expected: 'Missing an argument'
got: ''

I am not sure why it returns an empty string. I have looked at these posts with no luck:

If you need any more information about my script let me know. Thank you.

Upvotes: 1

Views: 1372

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59611

abort/exit will both print to stderr, whereas your rspec is listening on the stdout channel. Try the following:

expect(`ruby example_file.rb`).to output('Missing an argument').to_stderr

Upvotes: 1

Related Questions