如何期待Capybara :: Expectation for a method

时间:2017-01-17 06:05:19

标签: ruby exception capybara

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)

但仍然拖着那个例外 水豚:: ExpectationNotMet

2 个答案:

答案 0 :(得分:0)

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

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)

答案 1 :(得分:0)

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

解决了这个