symfony2.4中的Behat 3(学说访问)

Wis*_*bes 5 symfony doctrine-orm behat

情况

我想从现在开始在我的symfony项目中使用BDD和Behat.目前的项目是sf2.4,我努力使Behat 3工作.我使用 了关于behat3的最新文档,正如jakub在这篇文章中所推荐的那样.

问题

Behat 3似乎运作良好.但是,为了能够真正启动,我需要访问内核(容器,学说等).我尝试使用my_project,测试项目再现了Behat的示例.但是,使用$ this-> container(),$ this-> kernel-> 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
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图在AcmeDemoBundle中创建相同的behat'ls'测试:

|Acme 
    |Demo
        |Features
            |Context/FeatureContext.php
            ls.feature
Run Code Online (Sandbox Code Playgroud)

但是,它会引发错误:

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

解

这可能是由于使用了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: ~
Run Code Online (Sandbox Code Playgroud)

应用程序/ 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;
Run Code Online (Sandbox Code Playgroud)

Dai*_*shi 0

对于 behat 3.0.14 和 symfony 2.3,我需要在类的顶部添加这些语句FeatureContext:

use Behat\Symfony2Extension\Context\KernelAwareContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
Run Code Online (Sandbox Code Playgroud)

要素类还需要实现KernelAwareContext如下接口:

class FeatureContext extends MinkContext implements KernelAwareContext
Run Code Online (Sandbox Code Playgroud)

之后,我能够访问FeatureContext类中的 symfony 容器,如下面的步骤参数转换示例所示:

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