Behat无法识别已定义的步骤

时间:2015-06-17 21:59:24

标签: php behat mink

我遇到了Behat和Mink的问题。运行时我被提示在我的函数声明中添加#2。

这是我的作曲家文件版本信息

{
   "require": {
       "behat/behat": "2.5.*@stable",
       "behat/mink": "~1.6",
       "behat/mink-extension": "~1.0",
       "behat/mink-goutte-driver": "~1.1",
       "fabpot/goutte": "~1.0.4",
       "behat/mink-selenium2-driver": "*"
   },
   "config": {
       "bin-dir": "bin/"
   }
}

这是我的步骤定义

Given the user "XXX" has logged in using the password "YYYYY"

我在FeatureContext.php中创建了一个处理程序

/**
 * @Given /^the user "([^"]*)" has logged in using the password "([^"])"$/
 */
public function theUserHasLoggedInUsingThePassword($arg1, $arg2)
{
  ...
}

当我运行Behat时,我收到了消息

您可以使用以下代码段实现未定义步骤的步骤定义:

/**
 * @Given /^the user "([^"]*)" has logged in using the password "([^"])"$/
 */
public function theUserHasLoggedInUsingThePassword2($arg1, $arg2)
{
    $this->theUserHasLoggedInUsingThePassword($arg1,$arg2);
}

注意#2 添加到代码段中。

然后我添加这个片段

/**
 * @Given /^the user "([^"]*)" has logged in using the password "([^"])"$/
 */
public function theUserHasLoggedInUsingThePassword2($arg1, $arg2)
{
    throw new PendingException();
}

我在FeatureContext.php中同时拥有了UserHasLoggedInUsingThePassword和UserHasLoggedInUsingThePassword2函数

  

[贝哈特\贝哈特\异常\ RedundantException]

     

步骤" / ^我已经使用用户"([^"] *)"和密码   "([^"])" $ /"已定义于   FeatureContext :: iHaveLoggedInWithTheUserAndThePassword2()

     

FeatureContext :: iHaveLoggedInWithTheUserAndThePassword2()

     

FeatureContext :: iHaveLoggedInWithTheUserAndThePassword()

我觉得我遇到的RedundantException是一个红色的鲱鱼,真正的问题是我需要添加一个添加了2的函数。

有人看到我错过了什么吗?

3 个答案:

答案 0 :(得分:0)

摆脱第一个片段并使用第二个片段,抛出异常,因为正则表达式被定义了两次。方法名称无关紧要。然而,它没有意义,它没有第一次工作......

答案 1 :(得分:0)

从头开始并执行以下操作以查看会发生什么:

<强>缓存

app/console cache:clear
app/console cache:clear --env=test  (I guess you have test environment!)

<强>作曲家

"require-dev": {
        "behat/behat": "2.5.5",
        "behat/mink-extension": "1.3.3",
        "behat/mink": "1.5.0",
        "behat/symfony2-extension": "1.1.2",
        "behat/mink-selenium2-driver": "1.1.1",
        "behat/mink-browserkit-driver": "1.1.0",
        "behat/mink-goutte-driver": "1.0.9",

        "guzzle/guzzle": "3.9.2"
}

<强>步骤

When test login "hello" "word"

<强> FeatureContext

class FeatureContext extends MinkContext implements KernelAwareInterface
{
    /**
     * @When /^test login "([^"]*)" "([^"]*)"$/
     */
    public function testLogin($uid, $psw)
    {
        echo 'Step works fine';
    }
}

答案 2 :(得分:0)

经过几个小时的努力,我发现这是一个简单的错字/剪切和粘贴问题。

所以问题实际上是正则表达式语法。

/**
 * @Given /^the user "([^"]*)" has logged in using the password "([^"])"$/
 */

我正在使用Visual Studio(PHP Dev Tools)在wimp堆栈中执行此操作,并且我通过命令行运行它。在命令行上生成的输出看起来像这样

    /**
     * @Given /^the user "([^"]*)" has logged in using the password "([^"]
*)"$/
     */

将其粘贴到Visual Studio中时,最后一个正则表达式组中的*会捕捉到块注释。

/**
 * @Given /^the user "([^"]*)" has logged in using the password "([^"]
 *)"$/
 */

所以我错误地认为上一场比赛中的*是评论*不是正则表达式的一部分。这导致了我的代码段上方的以下行。

/**
 * @Given /^the user "([^"]*)" has logged in using the password "([^"])"$/
 */

而不是

/**
 * @Given /^the user "([^"]*)" has logged in using the password "([^"]*)"$/
 */

请注意唯一的区别是密码中的*“([^”] *)“$ /