Ruby 如何在Cucumber表(多行参数)中使用正则表达式来区分表?

Ruby 如何在Cucumber表(多行参数)中使用正则表达式来区分表?,ruby,regex,cucumber,Ruby,Regex,Cucumber,我正在使用一个场景表()来使用cucumber检查屏幕上的一些数据,使用内置的.diff!方法在表中列出 我想对照正则表达式检查内容匹配 Scenario: One Then the table appears as: | One | Two | Three | | /\d+/ | /\d+/ | /\d+/ | 实际的表可能看起来像 | One | Two | Three | | 123 | 456 | 789 | 这个场景被翻译成“只要有一些数字,我不

我正在使用一个场景表()来使用cucumber检查屏幕上的一些数据,使用内置的.diff!方法在表中列出

我想对照正则表达式检查内容匹配

Scenario: One
    Then the table appears as:
    | One   | Two   | Three |
    | /\d+/ | /\d+/ | /\d+/ |
实际的表可能看起来像

| One | Two | Three |
| 123 | 456 | 789   |
这个场景被翻译成“只要有一些数字,我不在乎”

失败的示例步骤实现:

Then /^the table appears as:$/ do |expected_table|
  actual_table  = [['One','Two', 'Three'],['123', '456', '789']]
  expected_table.diff! actual_table
end
错误:

Then the table appears as: # features/step_definitions/my_steps.rb:230
      | One    | Two    | Three  |
      | /\\d+/ | /\\d+/ | /\\d+/ |
      | 123    | 456    | 789    |
      Tables were not identical (Cucumber::Ast::Table::Different)
我曾尝试使用阶跃变换将单元格转换为正则表达式,但它们仍然不相同

转换代码:

 expected_table.raw[0].each do |column|
    expected_table.map_column! column do |cell|
      if cell.respond_to? :start_with?
        if cell.start_with? "/"
          cell.to_regexp
        else
          cell
        end
      else
        cell
      end
    end
  end
其中提供了eror:

Then the table appears as: # features/step_definitions/my_steps.rb:228
      | One          | Two          | Three        |
      | (?-mix:\\d+) | (?-mix:\\d+) | (?-mix:\\d+) |
      | 123          | 456          | 789          |
      Tables were not identical (Cucumber::Ast::Table::Different)

有什么想法吗?我被卡住了。

如果不编写自己的
diff实现,就没有办法做到这一点Ast::表中的方法。查看
cucumber/lib/ast/table.rb
。在内部,它使用库进行实际的比较,但不支持正则表达式匹配。

在场景中使用正则表达式几乎肯定是错误的方法。Cucumber特性旨在让关注业务的利益相关者阅读和理解

在更高的层次上编写步骤如何,例如:

Then the first three columns of the table should contain a digit

似乎您希望以一种提供酷炫的diff输出的方式编写此文件。否则,我会考虑这样写,您只需检查行。它不会那么漂亮,也不会让你了解整张桌子的不同,但它确实很重要

Then /^the table appears as:$/ do |expected_table|
  actual_table  = [['One','Two', 'Three'],['123', '456', '789']]

  expected_table.raw.each_with_index { |row, y|
    row.each_with_index { |cell, x| 
      actual_table[x][y].should == cell
    }
  }  
end

你能发布正在发生的错误吗?现在我想,这是有道理的。我仍然可以使用diff检查其他数据!谢谢谢谢,我已经看过了,但是我想知道是否有人知道一个快速的方法来改变这个。我想避免重写整件事谢谢迈克。但这不应该是一个~=?