Cakephp 3 - 运行phpunit测试时出现MissingDatasourceConfigException

时间:2015-06-12 10:22:58

标签: php unit-testing cakephp phpunit cakephp-3.0

我正在尝试使用PHPUnit 4.7.3在CakePHP 3中运行一些单元测试,但是我收到以下错误:

PHPUnit 4.7.3 by Sebastian Bergmann and contributors.

There was 1 error:

1) App\Test\TestCase\Model\Table\MoviesTableTest::testFindMoviesByGenre
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "default" was not found.

C:\xampp\htdocs\movie-pal\vendor\cakephp\cakephp\src\Datasource\ConnectionManager.php:188
C:\xampp\htdocs\movie-pal\vendor\cakephp\cakephp\src\ORM\TableRegistry.php:191
C:\xampp\htdocs\movie-pal\tests\TestCase\Model\Table\MoviesTableTest.php:17

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

我试过按照这本书,但可能我错过了一些东西。

在app.php中我有:

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'movies',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
    ],
    'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'test_movies',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
    ],
],

我的测试班:

namespace App\Test\TestCase\Model\Table;

use App\Model\Table\MoviesTable;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

class MoviesTableTest extends TestCase
{
    public $fixtures = ['app.movies'];

    public function setUp()
    {
        parent::setUp();
        $this->Movies = TableRegistry::get('Movies');
    }

    public function testFindMoviesByGenre()
    {
       .......
    }

}

phpunit.xml

 <?xml version="1.0" encoding="UTF-8"?>
<phpunit
  colors="true"
  processIsolation="false"
  stopOnFailure="false"
  syntaxCheck="false"
  bootstrap="./tests/bootstrap.php"
  >
  <php>
    <ini name="memory_limit" value="-1"/>
    <ini name="apc.enable_cli" value="1"/>
  </php>

  <!-- Add any additional test suites you want to run here -->
  <testsuites>
    <testsuite name="App Test Suite">
      <directory>./tests/TestCase</directory>
    </testsuite>
    <!-- Add plugin test suites here. -->
  </testsuites>

  <!-- 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>

</phpunit>

2 个答案:

答案 0 :(得分:1)

我在使用Ubuntu 14.04时遇到了同样的问题(这里没有PowerShell)。在我的情况下,问题是我从错误的位置运行测试。根据{{​​3}},您需要站在应用根目录的文件夹中才能正确运行测试。

所以在你的情况下,它会是这样的:

$ cd /path/to/your/app
$ vendor/bin/phpunit tests/TestCase/Model/Table/MoviesTable

答案 1 :(得分:0)

问题出在命令提示符中,当我在Windows PowerShell中运行测试时,一切正常。