设置故障退出代码

使用failure_exit_code选项在RSpec失败时设置自定义退出代码。

  RSpec.configure { |c| c.failure_exit_code = 42 }

背景

给定一个名为“spec/spec_helper.rb”的文件,其中包含

RSpec.configure { |c| c.failure_exit_code = 42 }

使用默认退出代码的失败规范

给定一个名为“spec/example_spec.rb”的文件,其中包含

RSpec.describe "something" do
  it "fails" do
    fail
  end
end

我运行rspec spec/example_spec.rb

那么退出状态应为1。

使用自定义退出代码的失败规范

给定一个名为“spec/example_spec.rb”的文件,其中包含

require 'spec_helper'
RSpec.describe "something" do
  it "fails" do
    fail
  end
end

我运行rspec spec/example_spec.rb

那么退出状态应为42。

运行使用自定义退出代码定义的规范spec时出错

给定一个名为“spec/typo_spec.rb”的文件,其中包含

require 'spec_helper'
RSpec.escribe "something" do # intentional typo
  it "works" do
    true
  end
end

我运行rspec spec/typo_spec.rb

那么退出状态应为42。

成功运行使用自定义退出代码定义的规范spec

给定一个名为“spec/example_spec.rb”的文件,其中包含

require 'spec_helper'
RSpec.describe "something" do
  it "works" do
    true
  end
end

我运行rspec spec/example_spec.rb

那么退出状态应为0。

当在 upstream 添加at_exit钩子时,使用默认退出代码退出

给定一个名为“exit_at_spec.rb”的文件,其中包含

require 'rspec/autorun'
at_exit { exit(0) }

RSpec.describe "exit 0 at_exit ignored" do
  it "does not interfere with the default exit code" do
    fail
  end
end

我运行ruby exit_at_spec.rb

那么退出状态应为1。