2013-12-20 46 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