可配置的颜色
RSpec 允许您配置文本格式化程序中使用的终端颜色。
failure_color
: 测试失败时使用的颜色(默认值::red
)success_color
: 测试通过时使用的颜色(默认值::green
)pending_color
: 测试待定时使用的颜色(默认值::yellow
)fixed_color
: 示例中的待定块通过但预期失败时使用的颜色(默认值::blue
)detail_color
: 用于各种测试详细信息的颜色(默认值::cyan
)
颜色指定为符号。选项为:black
、:red
、:green
、:yellow
、:blue
、:magenta
、:cyan
、:white
、:bold_black
、:bold_red
、:bold_green
、:bold_yellow
、:bold_blue
、:bold_magenta
、:bold_cyan
和 :bold_white
,
自定义失败颜色
给定一个名为“custom_failure_color_spec.rb”的文件,其中包含
RSpec.configure do |config|
config.failure_color = :magenta
config.color_mode = :on
end
RSpec.describe "failure" do
it "fails and uses the custom color" do
expect(2).to eq(4)
end
end
当我运行`rspec custom_failure_color_spec.rb --format progress`
那么失败的示例将以洋红色打印。
使用自定义控制台代码自定义失败颜色
给定一个名为“custom_failure_color_spec.rb”的文件,其中包含
RSpec.configure do |config|
config.failure_color = "1;32"
config.color_mode = :on
end
RSpec.describe "failure" do
it "fails and uses the custom color" do
expect(2).to eq(4)
end
end
当我运行`rspec custom_failure_color_spec.rb --format progress`
那么失败的示例将打印在“1;32”中。