如何期望 Capybara::Expectation 的方法

How to expect Capybara::Expectation for a method

def tiltle_finder(names)
  within 'table tbody tr:nth-child(1)' do
    page.should have_selector(
      'td:nth-child(10)',
      text: "#{names.label}"
    )
  end
end

it 'should find title' do
  expect{
    tiltle_finder(names)
  }.to raise_error(Capybara::ExpectationNotMet)

但仍然抛出异常 Capybara::ExpectationNotMet

您可以检查是否未满足预期,而不是挽救错误:

def have_title_finder(names)
  within 'table tbody tr:nth-child(1)' do
    have_selector(
      'td:nth-child(10)',
      text: "#{names.label}"
    )
  end
end

# ...
expect(page).not_to have_title_finder(names)
def tiltle_finder(names)
  within 'table tbody tr:nth-child(1)' do
    page.find('td:nth-child(10)', text: "#{names.label}")
  end
end

it 'should find title' do
  expect{
    tiltle_finder(names)
  }.to raise_error(Capybara::ElementNotFound)
end

这样解决