doushi8231 2013-12-20 10:02
浏览 60
已采纳

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

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"
},
  • 写回答

2条回答 默认 最新

  • dousi6087 2013-12-21 09:58
    关注

    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/"]}
    },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码