【发布时间】:2011-02-01 21:55:24
【问题描述】:
我不确定我在 CakePHP 单元测试配置中做错了什么。每次我运行一个测试用例时,我的测试数据库中都会丢失与我的夹具关联的模型表。
运行单个测试用例后,我必须使用 phpMyAdmin 重新导入我的数据库表。
以下是相关文件:
这是我要测试comment.php 的类。该表在测试后被删除。
App::import('Sanitize');
class Comment extends AppModel{
public $name = 'Comment';
public $actsAs = array('Tree');
public $belongsTo = array('User' => array('fields'=>array('id', 'username')));
public $validate = array(
'text' => array(
'rule' =>array('between', 1, 4000),
'required' =>'true',
'allowEmpty'=>'false',
'message' => "You can't leave your comment text empty!")
);
database.php
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'project.db',
'login' => 'projectman',
'password' => 'projectpassword',
'database' => 'projectdb',
'prefix' => ''
);
var $test = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'project.db',
'login' => 'projectman',
'password' => 'projectpassword',
'database' => 'testprojectdb',
'prefix' => ''
);
}
我的comment.test.php 文件。这是不断被丢弃的表。
<?php
App::import('Model', 'Comment');
class CommentTestCase extends CakeTestCase
{
public $fixtures = array('app.comment');
function start(){
$this->Comment =& ClassRegistry::init('Comment');
$this->Comment->useDbConfig = 'test_suite';
}
这是我的comment_fixture.php 类:
<?php
class CommentFixture extends CakeTestFixture
{
var $name = "Comment";
var $import = 'Comment';
}
以防万一,这里是 CommentTestCase 类中的一个典型测试方法
function testMsgNotificationUserComment(){
$user_id = '1';
$submission_id = '1';
$parent_id = $this->Comment->commentOnModel('Submission', $submission_id, '0', $user_id, "Says: A");
$other_user_id = '2';
$msg_id = $this->Comment->commentOnModel('Submission', $submission_id, $parent_id, $other_user_id, "Says: B");
$expected = array(array('Comment'=>array('id'=>$msg_id, 'text'=>"Says: B", 'submission_id'=>$submission_id, 'topic_id'=>'0', 'ack'=>'0')));
$result = $this->Comment->getMessages($user_id);
$this->assertEqual($result, $expected);
}
我已经处理了一天了,我开始对 CakePHP 的单元测试感到厌烦。除了这个问题,Servral 时代现在我已经在运行测试后通过“默认”数据库配置插入了数据!我的配置怎么了?!
【问题讨论】: