简体   繁体   English

将自定义服务注入Behat上下文

[英]Inject custom service into Behat context

Is it possible to build and inject my own, custom services into a Behat Context class? 是否可以将自己的定制服务构建并注入到Behat Context类中?

I'm integrating Behat with Symfony2, and I can inject Symfony's services into my contexts like so: 我将Behat与Symfony2集成在一起,可以将Symfony的服务注入到我的上下文中,如下所示:

contexts:
    - Meeebu\MyBundle\Features\Security\Context\SecurityContext:
        session:    '@session'

provided that the Behat\\Symfony2Extension is registered. 只要已注册Behat\\Symfony2Extension

I'm also using Behat Page Object Extension that injects Page objects into Context constructor directly, so my constructo currently looks like this: 我还使用了Behat Page Object Extension ,它直接将Page对象注入到Context构造函数中,因此我的当前构造看起来像这样:

public function __construct(Session $session, Homepage $homepage, WelcomeAdmin $welcomeAdminPage)

Now, I'd like to use my owe custom services in the Context class. 现在,我想在Context类中使用自己的自定义服务。

How can I create, register, and inject them in Behat? 如何在Behat中创建,注册和注入它们?

you need to create and register a behat extension: https://github.com/Behat/Behat/blob/2.5/src/Behat/Behat/Extension/ExtensionInterface.php 您需要创建并注册一个behat扩展名: https : //github.com/Behat/Behat/blob/2.5/src/Behat/Behat/Extension/ExtensionInterface.php

and then register your service (or load an yml/xml/etc. file) in the load method. 然后在load方法中注册您的服务(或加载yml / xml / etc。文件)。 and then add an extension class to the behat.yml, to the extensions section. 然后将扩展类添加到behat.yml的扩展部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM