Behat does not recognize a defined step

940 views Asked by At

I am having an issue with Behat and Mink. When running I am being prompted to add a #2 to my function declaration.

Here is my composer file for version info

{
   "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/"
   }
}

Here is my step definition

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

I have created a handler in FeatureContext.php

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

And when I run Behat I receive the message

You can implement step definitions for undefined steps with these snippets:

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

Please NOTE THE #2 being added to the snippet.

Then when I add this snippet

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

Having both theUserHasLoggedInUsingThePassword and theUserHasLoggedInUsingThePassword2 functions in the FeatureContext.php I recieve

[Behat\Behat\Exception\RedundantException]

Step "/^I have logged in with the user "([^"]*)" and the password "([^"])"$/" is already defined in FeatureContext::iHaveLoggedInWithTheUserAndThePassword2()

FeatureContext::iHaveLoggedInWithTheUserAndThePassword2()

FeatureContext::iHaveLoggedInWithTheUserAndThePassword()

I feel the RedundantException I am encountering is a red herring, the real issue is the fact that I need to add the function with a 2 added to it.

Anyone see anything I have missed?

3

There are 3 answers

0
muglio On BEST ANSWER

After hours of poking at this I discovered it was a simple typo/cut and paste problem.

So the issue is actually in the regular expression syntax.

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

I'm executing this in a wimp stack using Visual Studio (PHP Dev Tools) and I'm running this through the command line. The out put produced on the command line looks like this

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

Upon pasting it into Visual Studio the * in the last regex group snaps to the block comment.

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

So I mistakenly thought the * in the last match was a comment * not part of the regex. Which led to the following line above my snippet.

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

instead of

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

Please note the only difference is the * in the password "([^"]*)"$/

1
Anth Bieb On

Get rid of the first snippet and use the second, the exception is being thrown because the regex is being defined twice. The method name does not matter. However it does not make sense that it did not work the first time ...

0
BentCoder On

Start from scratch and do the following to see what happens:

cache

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

composer

"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"
}

Step

When test login "hello" "word"

FeatureContext

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