如何将 require_relative 与 rspec 测试一起使用?

How to use require_relative with an rspec test?

我有一个 rspec 测试,即描述和它

我想添加 before :allbefore :each 行为

我可以通过直接放入测试规范文件来添加它,即

describe "test" do
  before :all do
    CONTINUE_SPEC= true
  end

  around :each do |example|
    if CONTINUE_SPEC
      CONTINUE_SPEC = false
      example.run
      CONTINUE_SPEC = true unless example.exception
    else
      example.skip
    end
  end
  ... actual tests...

但是我想在几乎每个规范中都有它,所以我想我可以使用 require_relative 语句来简化它。但是,当我使用 require_relative 和

将代码传输到文​​件时
require_relative '../../support/continue_test_if_passing'

我明白了

Failure/Error:
  before :all do
    CONTINUE_SPEC= true
  end

NoMethodError:
  undefined method `before' for main:Object

只需将此添加到 RSpec.configure 块中的 spec_helper.rb

RSpec.configure do |config|
  config.before :all do
    CONTINUE_SPEC= true
  end

  config.around :each do |example|
    if CONTINUE_SPEC
      CONTINUE_SPEC = false
      example.run
      CONTINUE_SPEC = true unless example.exception
    else
      example.skip
    end
  end

  # ...
end