gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:15:58 25 4
gpt4 key购买 nike

情况
从现在开始,我想在我的 symfony 项目中使用 BDD 和 Behat。当前项目是 sf2.4,我努力使 Behat 3 工作。我用 the latest doc concerning behat3 ,正如 jakub 在 this post 中推荐的那样.
问题
Behat 3 似乎运行良好。但是,为了能够真正开始,我需要访问内核(容器、原则等...)。我尝试使用 my_project,测试项目为 Behat 重现 ls 示例。但是,使用 $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
);
}
}
我尝试在 AcmeDemoBundle 中创建相同的 behat 'ls' 测试:
|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: ~
应用程序/自动加载.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;

最佳答案

到目前为止,这一直对我有用。我为您的小 cucumber 添加了示例用法,因此它相当简单。

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
}
}

关于symfony - symfony2.4 中的 Behat 3(学说访问),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com