有没有办法在 RSpec 中首先运行嵌套的过滤器?

B S*_*ven 3 ruby rspec ruby-on-rails

describe 'Feature' do
  before do
    setup
  end

  describe 'Success' do
    before do
      setup_for_success
    end

    specify 'It works' do
      ...
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

RSpec 将始终运行setup之前的setup_for_success. 有没有办法先跑setup_for_success

Jam*_*ani 6

before(:all)您可以通过在尝试之前确定要运行的范围来做到这一点before(:each)

describe 'Feature' do
  before(:each) do
    puts "second"
  end

  describe 'Success' do
    before(:all) do
      puts "first"
    end

    specify 'It works' do
      ...
    end
  end
end

# =>
10:29:54 - INFO - Running: spec
Run options: include {:focus=>true}
first
second
.

Finished in 0.25793 seconds (files took 2.52 seconds to load)
1 example, 0 failures
Run Code Online (Sandbox Code Playgroud)

编辑:

在 Rspec 2 中,操作按以下顺序运行:

before suite
before all
before each
after each
after all
after suite
Run Code Online (Sandbox Code Playgroud)

以下是文档的链接,显示了调用方法的顺序: https: //www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks#块按顺序运行之前/之后

显然,在 Rspec 3.5 中,before 块调用具有不同的命名,但也有效。它们按以下顺序运行:

before :suite
before :context
before :example
after  :example
after  :context
after  :suite

describe 'Feature' do
  before(:example) do
    puts "second"
  end

  describe 'Success' do
    before(:context) do
      puts "first"
    end

    specify 'It works' do
      ...
    end
  end
end

10:59:45 - INFO - Running: spec
Run options: include {:focus=>true}
first
second
.

Finished in 0.06367 seconds (files took 2.57 seconds to load)
1 example, 0 failures
Run Code Online (Sandbox Code Playgroud)

这是较新的文档: http://www.relishapp.com/rspec/rspec-core/v/3-5/docs/hooks/before-and-after-hooks