2013-12-20 49 views
2

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

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

方案:

这一个是OK:

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

但是这样一来告诉我,我应该实现我的定义:

Scenario: Logout 
    Given I am logged in as "[email protected]" 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" 
}, 

回答

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.json的:

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

谢谢完美!我没有在文档中找到它,你有链接吗? – Tib

+1

here:http://docs.behat.org/guides/7.config.html#context –

0

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

您可以尝试运行​​并检查您的步骤定义是否已打印?

+0

我已经做到了,但我自定义的定义是不是在这里。 我有'给出/ ^(?: |我)在(?:| the)主页$ /'到'然后/ ^显示最后的回应$ /'但是没有'Given/^我登录为“ ^“] *)”with“([^”] *)“password $ /' – Tib