문제

I've installed Behat on my Symfony2 project but my custom definitions are not loaded, did I miss something?

behat.yml

default:
  paths:
    features: features
  extensions:
    Behat\Symfony2Extension\Extension:
      mink_driver: true
      kernel:
        env: test
        debug: true
    Behat\MinkExtension\Extension:
      base_url: 'http://myproject.local/app_test.php/'
      goutte: ~

features/bootstrap/FeatureContext.php

<?php

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

use Symfony\Component\HttpKernel\KernelInterface;
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Behat\Context\Step;

class FeatureContext extends MinkContext implements KernelAwareInterface
{

    private $kernel;
    private $parameters;

    /**
     * Initializes context.
     * Every scenario gets its own context object.
     *
     * @param array $parameters context parameters (set them up through behat.yml)
     */
    public function __construct(array $parameters)
    {
        // Initialize your context here
        $this->parameters = $parameters;
    }

    /**
     * Sets HttpKernel instance.
     * This method will be automatically called by Symfony2Extension ContextInitializer.
     *
     * @param KernelInterface $kernel
     */
    public function setKernel(KernelInterface $kernel)
    {
        $this->kernel = $kernel;
    }

    /**
    * @Given /^I am logged in as "([^"]*)" with "([^"]*)" password$/
    */
    public function iAmLoggedInAsWithPassword($username, $password)
    {
      return array(
          new Step\Given("I am on \"/login\""),
          new Step\When("I fill in \"Username\" with \"$username\""),
          new Step\When("I fill in \"Password\" with \"$password\""),
          new Step\When("I press \"Login\""),
      );
    }
}

Scenarios:

This one is ok:

  Scenario: Login success
    Given I am on "/login"
    ...

But this one tell me I should implement my definitions:

  Scenario: Logout
    Given I am logged in as "my@mail.com" with "password" password

error:

Vous pouvez implémenter les définitions d'étapes pour les étapes non définies avec ces modèles :

    /**
     * @Given /^I am logged in as "([^"]*)" with "([^"]*)" password$/
     */
    public function iAmLoggedInAsWithPassword($arg1, $arg2)
    {
        throw new PendingException();
    }

composer.json:

"require": {
    "behat/behat": "v2.5.1",
    "behat/mink": "v1.5.0",
    "behat/symfony2-extension": "v1.1.0",
    "behat/mink-extension": "v1.2.0",
    "behat/mink-selenium2-driver": "v1.1.1",
    "behat/mink-goutte-driver": "v1.0.9"
},
도움이 되었습니까?

해결책

Symfony2 extension not only changes the paths but also the namespace of a FeatureContext (it looks for context files in a bundle).

If you want to keep your features outside of the bundles, you have to specify the main context class (context.class):

default:
  paths:
    features: features
  context:
    class:  FeatureContext
  extensions:
    Behat\Symfony2Extension\Extension:
      mink_driver: true
      kernel:
        env: test
        debug: true
    Behat\MinkExtension\Extension:
      default_session: symfony2

You might also need to configure the composer autoloader to load the context files from features/bootstrap. To do that update the autoload section of your composer.json:

"autoload": {
    "psr-0": { "": ["src/", "features/bootstrap/"]}
},

다른 팁

Indeed, I don't see the problem here.

Can you try to run bin/behat -dl and check if your step definition is printed?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top