2014-05-13 88 views
5

形勢貝哈特3 symfony2.4內(教義接入)

我想用BDD和貝哈特在我的symfony項目,從現在開始。目前的項目是sf2.4,我努力讓Behat 3工作。我使用the latest doc concerning behat3,由jakub在this post推薦。

問題

貝哈特3似乎運作良好。但是,爲了能夠真正開始,我需要訪問Kernel(容器,教條等)。我試着用my_project,測試項目再現了Behat的例子。然而,使用這 - $>容器(),$這 - >內核級> getContainer()不變地引發一個 '現行異常'(代碼停止在iShouldGet步驟):

public function iShouldGet(PyStringNode $string) 
{ 
    //$container = $this->kernel->getContainer(); 
    //$container = $this->getContainer(); 
    //$doctrine = $this->getContainer()->get('doctrine'); 
    if ((string) $string !== $this->output) { 
     throw new Exception(
      "Actual output is:\n" . $this->output 
     ); 
    } 
} 

我試圖創建相同貝哈特'ls' 的內AcmeDemoBundle測試:

|Acme 
    |Demo 
     |Features 
      |Context/FeatureContext.php 
      ls.feature 

然而,它提出了一個錯誤:

[Behat\Testwork\Tester\Exception\WrongPathsException]  
No specifications found at path(s) `@AcmeDemoBundle`. 

這可能是由於使用Behat3,但我不確定。任何暗示爲什麼會出現這個問題/如何解決它?一般來說,關於如何整合behat到symfony2(2.4)項目的建議將非常值得讚賞。

非常感謝。

問候,


注意:這裏是我的文件:

behat.yml

# behat.yml 
default: 
    suites: 
     my_suite: 
      type: symfony_bundle 
      bundle: AcmeDemoBundle 
      mink_session: default 
      mink_javascript_session: selenium2 
    extensions: 
     #Behat\MinkExtension\Extension: 
     #Behat\MinkExtension\ServiceContainer\MinkExtension: 
     Behat\MinkExtension: 
      base_url: 'http://demo.com' 
      # this will be the url of our application 
      #base_url: 'http://wikipedia.org' 
      sessions: 
       default: 
        goutte: ~ 
       selenium2: 
        selenium2: ~ 
     Behat\Symfony2Extension: ~ 

應用程序/ autoload.php

<?php 

use Doctrine\Common\Annotations\AnnotationRegistry; 
use Composer\Autoload\ClassLoader; 

/** 
* @var ClassLoader $loader 
*/ 
$loader = require __DIR__.'/../vendor/autoload.php'; 

AnnotationRegistry::registerLoader(array($loader, 'loadClass')); 


$loader->add('Behat\Gherkin',realpath(__DIR__.'/../vendor/behat/gherkin/src')); 
$loader->add('Behat\Behat' ,realpath(__DIR__.'/../vendor/behat/behat/src')); 
$loader->add('Behat\BehatBundle' ,realpath(__DIR__.'/../vendor/bundles')); 

return $loader; 

回答

0

隨着貝哈特3.0。 14,和symfony 2.3,我需要添加在FeatureContext類的頂部SE聲明:

use Behat\Symfony2Extension\Context\KernelAwareContext; 
use Behat\Symfony2Extension\Context\KernelDictionary; 

的要素類還需要實現KernelAwareContext界面是這樣的:

class FeatureContext extends MinkContext implements KernelAwareContext 

在那之後,我能夠訪問symfony的容器中FeatureContext類似於以下示例中的step argument transfomation示例:

/** 
* @Transform :location 
*/ 
public function castAddressToLocation($location) 
{ 
    return $this->getContainer()->get('geocoder')->getLocation($location); 
} 
2

到目前爲止,這總是對我有用。我爲您的小黃瓜添加了示例用法,因此它非常簡單。

use Behat\MinkExtension\Context\MinkContext; 
use Behat\Symfony2Extension\Context\KernelAwareContext; 
use Symfony\Component\HttpKernel\KernelInterface; 

class FeatureContext extends MinkContext implements KernelAwareContext 
{ 
    protected $kernel; 

    public function setKernel(KernelInterface $kernelInterface) 
    { 
     $this->kernel = $kernelInterface; 
    } 

    /** 
    * @Given /^I can access service container$/ 
    */ 
    public function iCanAccessServiceContainer() 
    { 
     $container = $this->kernel->getContainer(); 
     return $container->getParameter('whatever'); 
    } 

    /** 
    * @Given /^I can access entity manager$/ 
    */ 
    public function iCanAccessEntityManager() 
    { 
     $em = $this->kernel->getContainer()->get('doctrine')->getManager(); 
     // So on 
    } 

    /** 
    * @Given /^I can access repository$/ 
    */ 
    public function iCanAccessRepository() 
    { 
     $em = $this->kernel->getContainer()->get('doctrine')->getManager(); 
     $repo = $em->getRepository('WhateverBundle:WhateverEntity'); 
     // So on 
    } 
}