无法在示例表中的字符串中插入空格

时间:2013-10-22 09:56:37

标签: ruby cucumber gherkin

Feature:player
@all

  Scenario Outline:Where is the player

    Given I navigate to Google
    When I enter < player> in the search field
    Then the text < keyword1> should be present

    @current @football
    Examples:
      | player  | keyword1   |
      | Rooney  | Manchester |
      | Gerrard | Liverpool  |
      | Terry   | Chelsea    |
    @old @football
    Examples:
      | player          | keyword1   |
      | Eric Cantona    | Manchester |

如果我写Cantona而不是Eric Cantona那么它可以工作,但是只要你运行程序时在字符串中插入了空格就会出错。

2 个答案:

答案 0 :(得分:2)

尝试在Scenario Outline占位符周围添加引号(并从占位符中删除前导空格)。例如:

Scenario Outline: Where is the player

  Given I navigate to Google
  When I enter "<player>" in the search field
  Then the text "<keyword1>" should be present

答案 1 :(得分:2)

问题是你的步骤定义只寻找一个单词:

When /^I enter (\w+) in the search field$/ do | player | 

Cucumber使用正则表达式将步骤与其定义相匹配,并捕获变量。您的步骤定义是查找"I enter ",后跟一个单词,后跟" in the search field"

您可以更改&#34;播放器&#34;从(\w+)([\w\s]+)的正则表达式。这将匹配单词和空格,并应匹配多字的例子。

When /^I enter ([\w\s]+) in the search field$/ do | player | 

或者,如果用引号包围变量(如orde所示),那么Cucumber应该使用(。*)组生成与引号内的任何内容匹配的步骤定义。