Rspec 水豚';在';跨多个测试的范围

Rspec 水豚';在';跨多个测试的范围,rspec,capybara,Rspec,Capybara,是否可以定义一个跨越多个测试的Capybara范围(在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-d

是否可以定义一个跨越多个测试的Capybara范围(在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 :each do |example|
  within('.some-div') do
    example.run
  end
end

虽然它会解析并运行,但实际上并没有将
范围内的
应用到示例中。

您应该能够这样做

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