0

我正在尝试在 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 = new CronJob();

$this->CronJob = ClassRegistry::init("CronJob");

加载夹具,我得到这个错误:

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

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

我正在使用 CakePHP 2.2.1、PHPUnit 3.6.12、PHP 5.4

4

1 回答 1

2

Fixtures 更喜欢小写的下划线约定。做这个:

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

因为它是一个插件;一定要ClassRegistry::init像这样使用插件符号:

$this->CronJob = ClassRegistry::init('Cron.CronJob');

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

于 2012-10-03T23:49:42.977 回答