2017-01-24 6 views

答えて

3

あなたはbefore(:each)がこれを試す前に実行するbefore(:all)をスコープによってこれを行うことができます。

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 

EDIT:

RSpecの2では、

この順序で実行するアクション:

before suite 
before all 
before each 
after each 
after all 
after suite 

ここに、メソッドの呼び出し順序を示すドキュメントへのリンクがあります。https://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks#before/after-blocks-are-run-in-order

明らかに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 

はここで新しいドキュメントです: http://www.relishapp.com/rspec/rspec-core/v/3-5/docs/hooks/before-and-after-hooks

+0

私はドキュメントを見たが、何も把握していません。 –

1

beforeフィルタは、指定されている順序で追加されます。 RSpec 2.10.0以降は、prepend_beforeフィルタにすることで、それらの前に追加することができます。

同様に、デフォルトではafterのフィルタがプリペンドされますが、代わりにappend_afterのフィルタを使用できます。次のように終わるでしょう

あなたのコード(簡潔にするために圧縮さ):

describe 'Feature' do 
    before { setup } 

    describe 'Success' do 
    prepend_before { setup_for_success } 

    it 'works' { ... } 
    end 
end