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

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

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\""),
      );
    }
}

场景:

这个还可以:

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

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

  Scenario: Logout
    Given I am logged in as "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="deb3a79eb3bfb7b2f0bdb1b3" rel="noreferrer noopener nofollow">[email protected]</a>" 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"
},

最佳答案

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

如果您想将功能保留在 bundle 之外,则必须指定主上下文类 (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/"]}
},

关于php - Behat + Symfony,未加载自定义定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700990/

相关文章:

php - 使用 autocomplete.js 中的值的 id 填充隐藏的输入

Symfony 2.1+ 在路由中编码散列/磅 "#"

php - Symfony2/Twig - 从动态数组键获取数组

Yaml 异常 : Unable to parse (near "apply_to: "\. CSS $"")

php - 如何访问 Behat 中值属性的间隔字符串?

session-cookies - 如何重新启动浏览器以保留 Behat 中的 cookie?

behat - 使用 Behat 预定义步骤的翻译(Phar 安装)

php - 实时更新图片

php - 指向数据库的符号链接(symbolic link)在/var/www 中不起作用

使用 NTLMv2 的 PHP 单点登录