Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring exit when running Rspec

Tags:

ruby

rspec

In Ruby, I need to print a feedback message and then exit with a status code of 1.

class Foo
  def bar
    puts "error"
    exit 1
  end
end

In RSpec, how can I ignore exit 1 when expecting the feedback message to be printed?

it "returns a feedback message" do
  expect { Foo.new.bar }.to output(/no error/i).to_stdout
end

If I run the spec above, no failure is raised.

Finished in 0.02402 seconds (files took 0.17814 seconds to load)
1 example, 0 failures
like image 312
Sig Avatar asked Dec 22 '25 22:12

Sig


1 Answers

exit raises a SystemExit error. You can rescue it like any other error or expect to raise_error:

RSpec.describe Foo do
  it "returns a feedback message" do
    expect { Foo.new.bar }
      .to output(/no error/i).to_stdout
      .and raise_error(SystemExit)
  end
end
like image 82
Alex Avatar answered Dec 25 '25 18:12

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!