muglio muglio - 5 months ago 21
PHP Question

Behat does not recognize a defined step

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?

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 "([^"]*)"$/

Comments