Behat + Symfony,未加载自定义定义 [英] Behat + Symfony, Custom definitions are not loaded
问题描述
我已经在我的Symfony2项目上安装了Behat,但是没有加载我的自定义定义,我错过了什么吗?
I've installed Behat on my Symfony2 project but my custom definitions are not loaded, did I miss something?
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\""),
);
}
}
场景:
这没关系:
Scenario: Login success
Given I am on "/login"
...
但是这个告诉我我应该实现我的定义:
But this one tell me I should implement my definitions:
Scenario: Logout
Given I am logged in as "my@mail.com" 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"
},
推荐答案
Symfony2扩展不仅更改路径,还更改FeatureContext的名称空间(它在捆绑中查找上下文文件).
Symfony2 extension not only changes the paths but also the namespace of a FeatureContext (it looks for context files in a bundle).
如果要将功能保留在捆绑软件之外,则必须指定主上下文类(context.class):
If you want to keep your features outside of the bundles, you have to specify the main context class (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自动加载器,以从 features/bootstrap 中加载上下文文件.为此,请更新 composer.json 的自动加载部分:
You might also need to configure the composer autoloader to load the context files from features/bootstrap. To do that update the autoload section of your composer.json:
"autoload": {
"psr-0": { "": ["src/", "features/bootstrap/"]}
},
这篇关于Behat + Symfony,未加载自定义定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!