2017-02-16 66 views
0

是否可以定義橫跨多個測試(在RSpec中)的水豚範圍?水豚跨範圍跨越多個測試

例如,我想關閉此:

it 'has two things' do 
    within('.some-div') do 
    expect(page).to have_text('foo') 
    expect(page).to have_text('bar') 
    end 
end 

成這樣:

context 'with two things' do 
    before :each do 
    within('.some-div') # pseudo-code - this doesn't work 
    end 

    it 'has foo' do 
    expect(page).to have_text('foo') 
    end 

    it 'has bar' do 
    expect(page).to have_text('bar') 
    end 
end 

我試圖使用around塊這樣的:

around :each do |example| 
    within('.some-div') do 
    example.run 
    end 
end 

雖然這解析並運行,它實際上並沒有將within作用域應用於示例。

回答

0

你應該能夠做到

before :each do 
    @section = page.find('.some-div') 
end 

it 'has foo' do 
    expect(@section).to have_text('foo') 
end 

it 'has bar' do 
    expect(@section).to have_text('bar') 
end