首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >behat,gherkin和regex:使用“否”或“不”

behat,gherkin和regex:使用“否”或“不”
EN

Stack Overflow用户
提问于 2016-03-31 10:31:27
回答 2查看 554关注 0票数 0

我有behat条件,我想用on函数来描述,使用"not":Given this is happeningGiven this is NOT happening。不过,我找不到一个有效的正则表达式。

以下是我想要达到的目标:

代码语言:javascript
复制
/**
 * @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$/

我想不出办法来做这个。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-31 12:05:20

你可以通过选择“not”来完成你想要做的事情:

代码语言:javascript
复制
/**
 * @Given /^this is( not)? happening$/
 */
public function thisIsHappening($not = null)
{
    if (null !== $not) {
        // do this
    }

    // do that anyway
}

您需要提供一个默认值(即null),因为这个参数不会出现在“正在发生的”步骤中。

但是,我会考虑让这两种方法变得更简单:

代码语言:javascript
复制
/**
 * @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();
}
票数 2
EN

Stack Overflow用户

发布于 2016-04-01 13:24:11

为了进一步得到公认的答案,您还可以使用以下内容:

代码语言:javascript
复制
/**
 * @Given /^this is(| not)? happening$/
 */
public function thisIsHappening($not)
{
    if ($not === " not") {
        // do this
    } else {
    // do that anyway
    }
}

它遵循与已接受的规则相同的规则,但也意味着您不必为$not设置默认值,因为它可能是“不”或空白。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36330866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档