【发布时间】: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 终端运行 phpunit 或 phpunit 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