How can I validate exit value in rspec? [duplicate]

萝らか妹 提交于 2019-12-24 03:01:52

问题


I have a method that will sometimes call exit(numeric_value).

Is it possible for rspec to validate that when the method is invoked, the process is exiting with the correct value?

I have seen these other posts, but they do not answer this specific question.

  • How can I validate exits and aborts in RSpec?
  • How to spec methods that exit or abort

回答1:


Given sample ruby code:

def it_will_exit
  puts "before exit"
  exit(false)
  puts "never get here"
end

rspec test case can be:

it "it must exit" do
  expect { it_will_exit }.raise_exception(SystemExit)
end

it "the exit value should be false" do
  begin
    it_will_exit
  rescue SystemExit=>e
    expect(e.status).to eq(1)
  end
end


来源:https://stackoverflow.com/questions/20432358/how-can-i-validate-exit-value-in-rspec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!