我有behat条件,我想用on函数来描述,使用"not":Given this is happening和Given this is NOT happening。不过,我找不到一个有效的正则表达式。
以下是我想要达到的目标:
/**
* @Given /^this is<regexCheckingNOT> happening$/
*/
public function thisIsHappening($not)
{
if ($not) {
// do this
}
// do that anyway
}我试过这些都没成功:
@Given /^this is(? NOT| ) happening$/@Given /^this is( )?(NOT)? happening$/我想不出办法来做这个。
发布于 2016-03-31 12:05:20
你可以通过选择“not”来完成你想要做的事情:
/**
* @Given /^this is( not)? happening$/
*/
public function thisIsHappening($not = null)
{
if (null !== $not) {
// do this
}
// do that anyway
}您需要提供一个默认值(即null),因为这个参数不会出现在“正在发生的”步骤中。
但是,我会考虑让这两种方法变得更简单:
/**
* @Given this is happening
*/
public function thisIsHappening()
{
// do that anyway
}
/**
* @Given this is not happening
*/
public function thisIsNotHappening()
{
// do this
// do that anyway if you need something to happen in both cases
$this->thisIsHappening();
}发布于 2016-04-01 13:24:11
为了进一步得到公认的答案,您还可以使用以下内容:
/**
* @Given /^this is(| not)? happening$/
*/
public function thisIsHappening($not)
{
if ($not === " not") {
// do this
} else {
// do that anyway
}
}它遵循与已接受的规则相同的规则,但也意味着您不必为$not设置默认值,因为它可能是“不”或空白。
https://stackoverflow.com/questions/36330866
复制相似问题