Behat + Symfony,未加载自定义定义

时间:2013-12-20 10:02:44

标签: php symfony behat mink

我已经在我的Symfony2项目上安装了Behat但是我的自定义定义没有加载,我错过了什么吗?

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: ~

功能/引导/ 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\""),
      );
    }
}

方案:

这个没问题:

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

但是这个告诉我应该实现我的定义:

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

错误:

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

2 个答案:

答案 0 :(得分:3)

Symfony2扩展不仅会更改路径,还会更改FeatureContext的命名空间(它会查找捆绑中的上下文文件)。

如果要将功能保留在捆绑包之外,则必须指定主要上下文类(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

您可能还需要配置composer自动加载器以从 features / bootstrap 加载上下文文件。为此,请更新 composer.json 的自动加载部分:

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

答案 1 :(得分:0)

确实,我在这里看不到问题。

您可以尝试运行bin/behat -dl并检查您的步骤定义是否已打印?