如何对Ruby应用程序进行单元测试以获取返回码

时间:2017-05-24 15:22:30

标签: ruby unit-testing

我对Ruby测试并不十分熟悉,而且我的搜索没有回答我的具体问题。

目前,我有一个应用程序,它会引发StandardError以在某些条件下退出。这样做的缺点是退出代码始终为1。

我想使用exit()为退出条件提供唯一代码。

目前,项目规范测试StandardError机制如下:

it "should raise an exception if no unignored project coverage file files were found" do
  fixtures_project.ignore_list = ["*fixturesTests*", "*fixtures*"]
  expect {fixtures_project.coverage_files}.to raise_error(StandardError)
end

我想做assert($?.exit == 102)之类的事情,但我仍需要事先触发fixtures_project.coverage_files才能获得退出代码。

我在它/ end内尝试了assert_equal(102, fixtures_project.coverage_files, "Hello there")的变体而没有运气。我确定这可能是简单的Ruby,但我还没有grokked这一点。

1 个答案:

答案 0 :(得分:2)

您尝试使用$?对象是对的。只需致电$?.exitstatus即可获得退出状态。

it "should raise an exception if no unignored project coverage file files were found" do
  fixtures_project.ignore_list = ["*fixturesTests*", "*fixtures*"]
  expect {fixtures_project.coverage_files}.to raise_error(StandardError)
  expect($?.exitstatus).to eq(102)
end