'while'循环中的Ruby'next'语句

时间:2016-02-26 08:35:53

标签: ruby loops cucumber

我正在尝试编写一个执行while循环的测试,最终将负责迭代遍历多个场景的1920循环。

如果一个循环失败,在这种情况下,文本不匹配,我希望循环移动到下一次迭代。

这是我正确迭代的while循环:

And(/^click on the endorsement to confirm court description from rows "(.*?)" to "(.*?)"$/) do |first_row, last_row|


data = CsvMapper.import('C:/auto_test_data/Courts code example csv.csv') do
    [dln, ni, pc, endorse, courtdesc]
  end


    cell = first_row.to_i - 1

while cell < last_row.to_i

  endorsement = data.at(cell).endorse
  courtdescription = data.at(cell).courtdesc

  find('li.category', text: endorsement).click

  court = first('li.offence-court', text: 'Court/Fixed penalty office')


  expect(court).to have_text courtdescription
  # add 'next' statement if this step fails

find('li.category', text: endorsement).click

cell = cell + 1


puts court.text



 end
  end

在那个循环中,我已经评论了失败点的位置,需要一些能够使循环移动到下一次迭代的东西。

此时错误处理是可选的。

对此的任何帮助都会很棒,因为我不想在黄瓜的1920次迭代中沿着示例表的路线走下去。

由于

1 个答案:

答案 0 :(得分:2)

防止断言未通过测试是一种奇怪的方法,如果您想通过一次测试报告多项内容,则表明您应该进行多次测试。但是坚持这个策略,它可以很好地构建一组不匹配的数据,然后在你完成时断言它是空的。然后,在正确的情况下,您的测试失败,并且您有一个列表,其中包含哪些迭代导致它。

failures = []
while cell < last_row.to_i
  # Gather your data
  failures << [cell, court] unless court.text.include?(courtdescription)
  # Prepare for the next iteration
end
expect(failures).to be_empty