如何在 rspec 中验证退出值? [英] How can I validate exit value in rspec?

查看:27
本文介绍了如何在 rspec 中验证退出值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有时会调用 exit(numeric_value) 的方法.

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

rspec 是否可以验证调用方法时,进程是否以正确的值退出?

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.

推荐答案

给定示例 ruby​​ 代码:

Given sample ruby code:

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

rspec 测试用例可以是:

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

这篇关于如何在 rspec 中验证退出值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆