php - Symfony2 Behat 扩展上下文将导致 "Step is already defined"

标签 php symfony testing behat mink

我正在尝试为应用程序编写一些 Behat 测试,我需要分离上下文,以便我可以在不同的其他上下文中使用一些核心元素,例如登录的用户步骤。

behat.yml

suites:
    common:
        type: symfony_bundle
        bundle: CommonBundle
        mink_session: symfony2
        mink_javascript_session: selenium2
        autoload:
            'CommonContext': %paths.base%/src/xxx/CommonBundle/Features/Context
        contexts: [ xxx\CommonBundle\Features\Context\CoreContext ]

    user:
        type: symfony_bundle
        bundle: UserBundle
        mink_session: symfony2
        mink_javascript_session: selenium2
        autoload:
            'UserContext': %paths.base%/src/xxx/UserBundle/Features/Context
        contexts:
            - Behat\MinkExtension\Context\MinkContext
            - xxx\CommonBundle\Features\Context\CoreContext
            - xxx\UserBundle\Features\Context\UserContext

DefaultContext.php

namespace XXX\CommonBundle\Features\Context;

use ...

/**
 * Class DefaultContext
 */
class DefaultContext extends MinkContext implements Context, KernelAwareContext
{

    /**
     * @param AbstractEntity $oEntity
     *
     * @return object
     */
    protected function getRepository(AbstractEntity $oEntity)
    {
        return $this->getService($oEntity);
    }

    /**
     * @return mixed
     */
    protected function getEntityManager()
    {
        return $this->getService('doctrine')->getManager();
    }

    /**
     * @param $id
     *
     * @return object
     */
    protected function getService($id)
    {
        return $this->getContainer()->get($id);
    }

CoreContext.php

namespace XXX\CommonBundle\Features\Context;

use ...

/**
 * Class SubContext
 */
class CoreContext extends DefaultContext implements Context, SnippetAcceptingContext
{

    /**
     * @Given I visit the homepage
     * @When I visit the homepage
     */
    public function iVisitHomepage()
    {
        $this->visitPath('/');
    }

    /**
     * @And /^I am logged in as "([^"]*)"$/
     * @Then /^I am logged in as "([^"]*)"$/
     */
    public function iAmLoggedInAs($username)
    {
        $user = $this->getContainer()->get('fos_user.user_manager')->findUserByUsername($username);

        $this->visit('/login');

        $this->fillField('_username', $user->getEmailCanonical());
        $this->fillField('_password', self::USER_PASSWORD);

        $this->pressButton('_submit');
    }

UserContext.php

namespace xxx\UserBundle\Features\Context;

use ...

/**
 * Defines application features from the specific context.
 */
class UserContext extends CoreContext implements Context, SnippetAcceptingContext
{

    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {
    }

}

register_user.feature

Feature: Register user

  @javascript
  Scenario: Register user
    Given I am on homepage
    And I go to "/register/"
    And I am logged in as "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03656c6c436162712d606c6e" rel="noreferrer noopener nofollow">[email protected]</a>"
    Then I should see "Terms and Conditions"

因此,当我运行测试时,我收到一条错误消息:

Given I am on homepage
      Step "I visit the homepage" is already defined in xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()

      xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
      xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
    And I go to "/register/"
      Step "I visit the homepage" is already defined in xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()

      xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()
      xxx\CommonBundle\Features\Context\CoreContext::iVisitHomepage()

我的理解完全错误还是我在这里缺少一些设置?

最佳答案

不要扩展提供步骤定义的上下文。没有办法解决这个问题。

好的扩展为上下文提供了方便的方法,但没有步骤定义。对于 Mink 扩展,在 MinkContext 旁边还有 RawMinkContext。首先提供步骤定义,不应扩展。另一个提供了您可能感兴趣的辅助方法。RawMinkContext 是您应该扩展的方法。或者,您也可以使用 MinkAwareTrait

关于php - Symfony2 Behat 扩展上下文将导致 "Step is already defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35388724/

相关文章:

php - 扫描目录中的php文件,并将它们包含在当前脚本中

php - 通过 OpenCart addOrder 方法处理订单

php - 在 PHP 的 OOP 编程中设置我的类的正确方法是什么?

php - 如何从包外部访问 Symfony 2 包配置?

ruby - 当多个共享相同的名称和 ID 时,如何填写 Webrat 中的特定字段?

javascript - PHP 中的查询语句有未定义的参数

Nelmio Alice 的 Symfony 4 Fixtures 不持久

php - Doctrine2 不可变实体和仅附加数据结构

android - 是否可以将 Android 应用程序设置为连接到开发服务器?

testing - 早期测试会导致过早优化吗?