为什么我的CakePHP测试夹具不工作?

为什么我的CakePHP测试夹具不工作?,cakephp,phpunit,fixtures,Cakephp,Phpunit,Fixtures,我试图在CakePHP应用程序中测试插件模型。该模型使用一个名为“cron_jobs”的表,我为该表设置了以下装置: class CronJobFixture extends CakeTestFixture { public $import = array('table' => 'cron_jobs'); } 我的测试类运行良好,如下所示: <?php App::uses('CronJob', 'Cron.Model'); class CronJobTest exten

我试图在CakePHP应用程序中测试插件模型。该模型使用一个名为“cron_jobs”的表,我为该表设置了以下装置:

class CronJobFixture extends CakeTestFixture
{
    public $import = array('table' => 'cron_jobs');
}
我的测试类运行良好,如下所示:

<?php

App::uses('CronJob', 'Cron.Model');

class CronJobTest extends CakeTestCase
{

    public $fixtures = array('plugin.cron.CronJob');

    public function setUp()
    {
        parent::setUp();
        $this->CronJob = new CronJob();
    }

    public function testCollectFailsOnMissingComponent()
    {
        $this->setExpectedException('InvalidArgumentException');
        $this->CronJob->collect(null);
    }

    public function testCollectSilentOnMissingComponent()
    {
        $result = $this->CronJob->collect('SomeNonExistingComponent');
        $this->assertEquals(null, $result);
    }

    // Some more tests that will need the fixture ....
}
$this->CronJob = ClassRegistry::init('Cron.CronJob');

$this->CronJob = ClassRegistry::init("CronJob");
要加载夹具,我会遇到以下错误:

CronJobTest::testCollectSilentOnMissingComponent PDO异常: SQLSTATE[42000]:语法错误或访问冲突:1064您有 SQL语法错误;检查与您的产品相对应的手册 在“collect”附近使用正确语法的MySQL服务器版本 第1行

CronJob类中的任何内容都不能生成错误,因为这两个测试所涵盖的代码不访问数据库。我确信我的测试数据库配置正确,因为如果我更改测试数据库配置,会出现数据库连接错误


我使用的是CakePHP2.2.1、PHPUnit 3.6.12、PHP5.4,我更喜欢使用小写下划线的约定。这样做:

public $fixtures = array('plugin.cron.cron_job');
因为它是一个插件;请确保将插件符号与
ClassRegistry::init
一起使用,如下所示:

<?php

App::uses('CronJob', 'Cron.Model');

class CronJobTest extends CakeTestCase
{

    public $fixtures = array('plugin.cron.CronJob');

    public function setUp()
    {
        parent::setUp();
        $this->CronJob = new CronJob();
    }

    public function testCollectFailsOnMissingComponent()
    {
        $this->setExpectedException('InvalidArgumentException');
        $this->CronJob->collect(null);
    }

    public function testCollectSilentOnMissingComponent()
    {
        $result = $this->CronJob->collect('SomeNonExistingComponent');
        $this->assertEquals(null, $result);
    }

    // Some more tests that will need the fixture ....
}
$this->CronJob = ClassRegistry::init('Cron.CronJob');

发生此错误的原因是CakePHP在没有
collect()
方法的情况下延迟加载非插件
CronJob
模型。

!你真的很懂你的东西。