如何在列表(列)而不是行中读取黄瓜表

时间:2015-03-26 01:08:48

标签: ruby cucumber

在Cucumber功能中,我们可以使用表格来收集数据,如下所示

    Then I receive the following errors
        | 10 |
        | 11 |
        | 12 |

然后我的步骤如下。

Then(/I receive the following errors$/) do |table|
  data = table.raw
  data.each do |entry|
    status = entry[0]
      # : Do whatever I like
  end
end

然而,鉴于它只是一个数据列表,我打算用这种特征格式代替

    Then I receive the following errors
        | 10 | 11 | 12 |

我不知道名单会有多长。如何编写我的步骤定义以遍历表列表?

感谢。

3 个答案:

答案 0 :(得分:4)

找到了可以满足行和列的解决方案。

data = table.raw
data.each do |rowdata|
  rowdata.each do |entry|
    status = entry
      # : Do whatever I like
  end
end

答案 1 :(得分:0)

使用逗号分隔列表。 然后我收到以下错误

"""
10, 11, 12, 14
"""

使用此代码

Then(^I recieve the following error$) |errors|
  list = errors.split(/,\s/)

答案 2 :(得分:0)

DataTable与一列转换为值列表的另一种方法。

Then I receive the following errors
  | 10 |
  | 11 |
  | 12 |

Then(/^I receive the following errors$/) do |table|

    expected = table.cells_rows.map {|row| row(0)} # => [10, 11, 12]

    # do assertions ...
end

cells_rows将返回行(类型为Cells)的集合,其中可以通过索引访问单元格。如果一列索引始终为0