黄瓜测试的步骤定义应该能够处理“a”和“an”[正则表达相关]

时间:2012-09-12 17:13:00

标签: ruby regex cucumber

我有这两个测试。他们几乎都做同样的事情,所以我不想写两个单独的步骤做同样的事情。

Scenario: I delete a question link
  Given   I am on the home page
    When  I "delete" a "question" link
    Then  A "question" link should be "deleted"

Scenario: I delete a account link
  Given   I am on the home page
    When  I "delete" an "account" link
    Then  An "account" link should be "deleted"

如果我想编写一个适用于I "delete" a "question" linkI "delete" an "account" link的步骤,我将如何修改以下正则表达式以处理“a”和“an”。

/^I "([^"]*)" a/an?? "([^"]*)" link$/

1 个答案:

答案 0 :(得分:4)

这应该做:

/^I "([^"]*)" an? "([^"]*)" link$/

问号表示“n”字符可以是字符串的一部分但不必

如果类型中有任何“值”,您也可以使用:

/^I "([^"]*)" (a|an) "([^"]*)" link$/