【问题标题】:Behat not autoloading additional classes to a test contextBehat 不会将其他类自动加载到测试上下文中
【发布时间】:2014-08-06 18:12:35
【问题描述】:

Behat 版本 3.0.12,PHP 5.3.3

我将在展示我的设置之前描述问题:我正在创建一个小型测试 Behat 项目,但是当我尝试在其他文件中创建其他类以拉入我的上下文文件时,Behat 不会自动加载它们。文档说features/bootstrap 中的任何内容都应该自动加载并可用。具体错误是这样的:

Fatal error: Class 'TestObject' not found in /data/drupal7/sites/behat-test/projects/test-project/features/bootstrap/TestContext.php on line 23

version 3 docs 中,本教程在一个新的 PHP 文件中定义了一个类并毫不费力地将其引入。尝试在我的版本上做同样的事情是行不通的。

我有这样的 Behat 设置:

composer.*
projects
vendor

这是安装 Behat 的标准结果,除了有一个新文件夹,我的各个项目在 project/<project name> 中有自己的文件夹。比如project/test_project:

behat.yml
features
    |-- test.feature
    |-- bootstrap
           |-- TestContext.php
           |-- Test.php

我的配置文件behat.yml 如下所示:

default:
    suites:
        default:
            paths:     [ %paths.base%/features ]
            bootstrap: [ $paths.features%/bootstrap ]
            contexts:  [ TestContext ]

TestContext.php 看起来像这样:

<?php

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
 * Behat context class.
 */
class TestContext implements Context
{
    private $test;

    public function __construct()
    {
        $this->test = new TestObject();
    }

    /**
     * @Given I go to the Google homepage
     */
    public function iGoToTheGoogleHomepage()
    {
        throw new PendingException();
    }
}

最后,Test.php

<?php

final class TestObject {
    private $price = 0;

    public function setPrice($amount) {
        $this->price = $amount;
    }

    public function getPrice() {
        return $this->price;
    }
}

有什么想法吗?

【问题讨论】:

    标签: php behat


    【解决方案1】:

    事实证明,其他文件需要遵循 PSR-0 标准 - 在这种情况下,TestObject 类需要位于同名文件中,例如TestObject.php

    【讨论】:

    • 网址已弃用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多