代码之家  ›  专栏  ›  技术社区  ›  Michael Durrant

如何在rspec测试中使用require\u relative?

  •  -1
  • Michael Durrant  · 技术社区  · 7 年前

    我有rspec测试,即描述和it

    我想补充一下 before :all before :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\u relative将代码传输到文件时

    require_relative '../../support/continue_test_if_passing'
    

    我明白了

    Failure/Error:
      before :all do
        CONTINUE_SPEC= true
      end
    
    NoMethodError:
      undefined method `before' for main:Object
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   spickermann    7 年前

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

    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