ingDatasourceConfigException运行ph

ingDatasourceConfigException运行ph

本文介绍了Cakephp 3 - MissingDatasourceConfigException运行phpunit测试时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  PHPUnit 4.7.3由Sebastian Bergmann和贡献者。 

有1个错误:

1)App\Test\TestCase\Model\Table\MoviesTableTest :: testFindMoviesByGenre
Cake \Datasource \Exception\MissingDatasourceConfigException:未找到数据源配置default。

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

失败!
测试:1,断言:0,错误: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,
],
],

我的测试类:

 命名空间App\Test\TestCase\Model\Table; 

使用App\Model\Table\MoviesTable;
使用Cake\ORM\TableRegistry;
使用Cake \TestSuite\TestCase;

类MoviesTableTest扩展TestCase
{
public $ fixtures = ['app.movi​​es'];

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

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

}

phpunit.xml

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

<! - 添加您要在此处运行的任何其他测试套件 - >
< testsuites>
< testsuite name =App Test Suite>
< directory> ./ tests / TestCase< / directory>
< / testsuite>
<! - 在这里添加插件测试套件。 - >
< / testsuites>

<! - 设置夹具的监听器 - >
< 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>


解决方案

问题出现在命令提示符我在Windows PowerShell中的测试一切正常。


I am trying to run some unit tests in CakePHP 3 with PHPUnit 4.7.3, but I`m getting the following error:

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.

I have tried to follow the book, but probably I`m missing something.

In the app.php I have:

'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,
    ],
],

My test class:

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>
解决方案

The problem was in the Command prompt, when I run my tests in Windows PowerShell everything was OK.

这篇关于Cakephp 3 - MissingDatasourceConfigException运行phpunit测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:02