Behat featurecontext 可重用操作正则表达式问题

Behat featurecontext reusable action regex issue

我正在尝试在我的功能上下文中编写一个函数,其中包含操作中的可选部分。

/**
* Function to scroll into view.
*
* @When /^I scroll "([^"]*)" into view( of type "([^"]*)")?$/g
*/
public function iScrollIntoView($locator, $type = "id") {
    // Some logic here.
}

所以我的想法是我可以在我的 behat 脚本中使用:I scroll "foo" into viewI scroll "foo" into view of type "bar"

所以我希望类型部分是可选的,而正则表达式似乎在在线正则表达式测试器中运行良好,但在我的 behat 脚本中似乎不起作用。

当我将 And I scroll "foo" into view 放入 behat 脚本时,它无法识别该函数。我的正则表达式有问题吗?或者这是一个行为问题。

经过一番深思熟虑,解决方案非常合乎逻辑。导致 behat 失败的主要问题是正则表达式中的 global => g,因此在删除它之后 behat 正在识别操作。

/**
* Function to scroll into view.
*
* @When /^I scroll "([^"]*)" into view( of type "([^"]*)")?/
*/

第二个问题是分组,在我的函数中我使用第二组作为我的参数,但是这是可选的组("foo" 类型)并且实际上不是该组的变量.所以在向函数添加第三个参数后,我能够检索它。

public function iScrollIntoView($locator, $of_type_provided = FALSE, $type = "id") {
    // Some logic here.
}

我希望这对以后的其他人有用。