【问题标题】:Why is the "default" datasource connection missing when running tests?为什么运行测试时缺少“默认”数据源连接?
【发布时间】:2017-03-20 19:19:20
【问题描述】:

我正在使用 here 所述的固定装置编写测试。

我的测试引导程序:

if (!getenv('db_dsn')) {
    putenv('db_dsn=sqlite:///:memory:');
}

ConnectionManager::config('test', ['url' => getenv('db_dsn')]);
ConnectionManager::config('test_custom_i18n_datasource', ['url' => getenv('db_dsn')]);

我的代码:

use Cake\TestSuite\TestCase;

class BackupExportTest extends TestCase
{
    public $fixtures = ['core.articles', 'core.comments'];

    public function setUp()
    {
        parent::setUp();
        $this->Articles = \Cake\ORM\TableRegistry::get('Articles');
    }

    public function testMyFunction()
    {
        $query = $this->Articles->find('all');
    }
}

现在,运行测试,这是个例外:

PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 62 ms, Memory: 4.00MB

There was 1 error:

1) MysqlBackup\Test\TestCase\Utility\BackupExportTest::testMyFunction
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "default" was not found.

/home/mirko/Libs/Plugins/cakephp-mysql-backup/vendor/cakephp/cakephp/src/Datasource/ConnectionManager.php:196
/home/mirko/Libs/Plugins/cakephp-mysql-backup/vendor/cakephp/cakephp/src/ORM/Locator/TableLocator.php:175
/home/mirko/Libs/Plugins/cakephp-mysql-backup/vendor/cakephp/cakephp/src/ORM/TableRegistry.php:110
/home/mirko/Libs/Plugins/cakephp-mysql-backup/tests/TestCase/Utility/BackupExportTest.php:38

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

所以,\Cake\ORM\TableRegistry::get() 似乎在搜索 default 连接。为什么?如何解决?


编辑

phpunit.xml.dist 在这里:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    colors="true"
    processIsolation="false"
    stopOnFailure="false"
    syntaxCheck="false"
    bootstrap="./tests/bootstrap.php"
    >

    <testsuites>
        <testsuite name="cakephp-mysql-backup Test Cases">
            <directory>./tests/TestCase</directory>
        </testsuite>
    </testsuites>

    <!-- configure code coverage -->
    <filter>
        <whitelist>
            <directory suffix=".php">./src/</directory>
        </whitelist>
    </filter>
</phpunit>

通过 nix 终端运行 phpunitphpunit tests/TestCase/Utility/BackupExportTest.php 会引发异常。 phpunit/usr/bin/phpunit 上并通过 deb 包安装。

【问题讨论】:

  • 您用来运行 phpunit 的命令到底是什么样的?你到底从哪里运行它?还有你的phpunit.xml 文件是什么样的?
  • @ndm 我不明白你的第一个问题。文件 phpunit.xml.dist 为 here
  • 请将该代码添加到您的问题中,以便将来的读者可以独立使用。对于我的第一个问题,我询问您用于运行 phpunit 的 CLI 命令,以及您正在运行的文件系统中的何处以及通过哪种 CLI(nix 终端、Windows 默认 CLI、Windows Powershell 等)它。
  • 好的@ndm,我修改了原帖

标签: cakephp testing phpunit cakephp-3.0


【解决方案1】:

通常你最好需要 PHPUnit 作为依赖并通过vendor/bin/phpunit 运行它,但实际问题很可能是你没有配置夹具监听器。

引用自文档:

在您可以使用固定装置之前,您应该仔细检查您的 phpunit.xml 包含夹具监听器:

<!-- Setup a listener for fixtures -->
<listeners>
        <listener
        class="\Cake\TestSuite\Fixture\FixtureInjector"
        file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
                <arguments>
                        <object class="\Cake\TestSuite\Fixture\FixtureManager" />
                </arguments>
        </listener>
</listeners>

夹具管理器负责创建正确的连接别名,即将非测试连接请求映射到test* 连接,例如defaulttest

另见

【讨论】:

  • 是的,这行得通。另一件事:确保您的固定装置是可加载的。就我而言,我使用的是核心固定装置。所以,在我的composer.json"Cake\\Test\\": "vendor/cakephp/cakephp/tests"
猜你喜欢
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多