doushi8231 2013-12-20 10:02
浏览 60
已采纳

Behat + Symfony,未加载自定义定义

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: ~

features/bootstrap/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\""),
      );
    }
}

Scenarios:

This one is ok:

  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

error:

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"
},
  • 写回答

2条回答 默认 最新

  • dousi6087 2013-12-21 09:58
    关注

    Symfony2 extension not only changes the paths but also the namespace of a FeatureContext (it looks for context files in a bundle).

    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
    

    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/"]}
    },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀