Ruby 带有逗号'的正则表达式问题;s告诉我有6个参数,而不是4个

Ruby 带有逗号'的正则表达式问题;s告诉我有6个参数,而不是4个,ruby,rspec,cucumber,watir-webdriver,Ruby,Rspec,Cucumber,Watir Webdriver,我有一个场景大纲表,如下所示: Scenario Outline: Verify Full ad details Given I am on the xxx classified home page And I have entered <headline> in the search field & clicked on search When I click on full details Then I should see <hea

我有一个场景大纲表,如下所示:

Scenario Outline: Verify Full ad details
    Given I am on the xxx classified home page
    And I have entered <headline> in the search field & clicked on search
    When I click on full details
    Then I should see <headline> <year> <mileage> <price> displaying correctly and successfully

    Examples:
    |headline               |year   |mileage    |price      |
    |alfa romeo 166         |2005   |73,000     |6,990      |
场景大纲:验证完整的广告详细信息
鉴于我在xxx分类主页上
我已在搜索字段中输入并单击搜索
当我点击全部细节时
然后我将看到正确且成功地显示
示例:
|标题|年份|里程|价格|
|阿尔法罗密欧166 | 2005 | 73000 | 6990|
当我运行我的场景时,它显示我有6个参数。 但我想,我应该只有4个参数:标题、年份、里程和价格。 我认为它是把逗号和它前后的内容作为两个独立的参数

有没有办法让cucumber认为下面的示例中只有4个arg

我已经考虑过和regex混日子了,但我似乎没什么进展。
非常感谢您的帮助。

由于73000/6990只是一个int,您可以将其作为73000/6990输入(不带逗号)吗?我不认为这会造成问题,除非您出于某种原因需要这种格式?

在这种情况下,格式化数字的好方法是使用下划线,而不是逗号或句点。您可以避免区域设置问题(1.000对1000),并保留一些格式

|headline               |year   |mileage    |price      |
|alfa romeo 166         |2005   |73_000     |6_990      |

我想这是你遇到问题的最后一步

当我尝试最后一步时,给出的默认步骤是:

Then /^I should see alfa romeo (\d+) (\d+) (\d+),(\d+) (\d+),(\d+) displaying correctly and successfully$/ do |arg1, arg2, arg3, arg4, arg5, arg6|
  pending # express the regexp above with the code you wish you had
end
我不确定Cucumber是如何确定建议步骤定义的,但这并不适用于您。正如你所说,有6个论点是错误的。同样,这一步骤将只匹配标题以“阿尔法·罗密欧”开头的例子

将步骤定义更改为以下内容将解决当前场景示例:

Then /^I should see (.+) (\d+) ([\d|,]+) ([\d|,]+) displaying correctly and successfully$/ do |arg1, arg2, arg3, arg4|
    puts arg1   #=> alfa romeo 166
    puts arg2   #=> 2005
    puts arg3   #=> 73,000
    puts arg4   #=> 6,990
end
两个变化:

1) 逗号格式的数字已更改为:

([\d|,]+)
这意味着它将匹配任何数字或逗号字符。如果您的数字也可以包含小数,您将需要以下内容:

([\d|,|.]+)

2) 标题也被更改了,这样可以得到整个标题,而不仅仅是结束数字。

您是否将结果输出到csv中,然后用逗号拆分?这就解释了为什么你会得到6个参数,
73000
6990
被分成4个独立的数字。请注意,这个解决方案假设你只使用逗号格式的数字。如果你也在测试法语(比如数字“1000,20”),那么其他解决方案中的一个会更好。谢谢Justin,你的解决方案对我有用。我将在周末发布我的步骤定义。再次感谢您,我需要逗号格式,因为我想验证前端的值