自定义弃用流

定义一个用于警告弃用的自定义输出流 (默认 $stderr)。

  RSpec.configure do |c|
    c.deprecation_stream = File.open('deprecations.txt', 'w')
  end

  RSpec.configure { |c| c.deprecation_stream = 'deprecations.txt' }

或传递 --deprecation-out

背景

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

class Foo
  def bar
    RSpec.deprecate "Foo#bar"
  end
end

默认 - 将弃用打印到 $stderr

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

require "foo"

RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

我运行 rspec spec/example_spec.rb

那么 输出应该包含“弃用警告:\n\nFoo#bar 已弃用”。

使用文件路径配置

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

require "foo"

RSpec.configure {|c| c.deprecation_stream = 'deprecations.txt' }

RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

我运行 rspec spec/example_spec.rb

那么 输出应该不包含“弃用警告:”

但是 输出应该包含“1 个弃用已记录到 deprecations.txt”

并且 文件“deprecations.txt”应该包含“Foo#bar 已弃用”。

使用 File 对象配置

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

require "foo"

RSpec.configure do |c|
  c.deprecation_stream = File.open('deprecations.txt', 'w')
end

RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

我运行 rspec spec/example_spec.rb

那么 输出应该不包含“弃用警告:”

但是 输出应该包含“1 个弃用已记录到 deprecations.txt”

并且 文件“deprecations.txt”应该包含“Foo#bar 已弃用”。

使用 CLI --deprecation-out 选项配置

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

require "foo"
RSpec.describe "calling a deprecated method" do
  example { Foo.new.bar }
end

我运行 rspec spec/example_spec.rb --deprecation-out deprecations.txt

那么 输出应该不包含“弃用警告:”

但是 输出应该包含“1 个弃用已记录到 deprecations.txt”

并且 文件“deprecations.txt”应该包含“Foo#bar 已弃用”。