CakePHP测试夹具运行测试用例后永久性地删除我的表

时间:2010-03-29 05:01:06

标签: cakephp testing

我不确定我在CakePHP单元测试配置中做错了什么。每次运行测试用例时,我的测试数据库中都会缺少与我的fixture相关的模型表。

运行单个测试用例后,我必须使用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时间现在我已经在运行测试后通过'default'数据库配置插入了数据!我的配置发生了什么?!

3 个答案:

答案 0 :(得分:4)

在CakeTestCase类中,只需添加一个成员 dropTables

<?php
App::import('Model', 'Comment');

class CommentTestCase extends CakeTestCase
{

  public $fixtures = array('app.comment');
  public $dropTables = false; // <- here it is 

  function start(){
    $this->Comment =& ClassRegistry::init('Comment');
    $this->Comment->useDbConfig = 'test_suite';
  }

答案 1 :(得分:3)

我认为这就是主意。灯具是具有固定内容的固定表格。他们您在生产或开发过程中使用的表格。这就是为什么他们在database.php中有一个单独的连接,你应该使用不同的数据库或至少为测试表使用不同的前缀。这些将在每次测试之前根据您的灯具创建并在之后拆除,因此您始终会针对一组已知数据进行测试。

答案 2 :(得分:2)

您的数据库配置看起来正确......

你不应该这样做

$this->Comment->useDbConfig = 'test_suite';

我的推荐

您应该使用cake bake shell为所有模型创建模型,并让它覆盖它们(显然首先备份所有自定义模型)。您不需要通过验证和关联提示(例如“n”),但是对所有覆盖都是肯定的(再次,首先备份您的代码)。

当您这样做时,它还会为模型创建一个基本测试用例和夹具。

使用它创建的测试用例,并添加到它...

你需要的是然后调用你使用的每个夹具和所有相关的夹具......所有hasMany和belongsTo和HABTM关联都需要加载夹具,但它们不会自动被调用进入测试套件。

<强> note.test.php

<?php 
App::import('Model', 'Note');
class NoteTestCase extends CakeTestCase {
    var $Note = null;
    var $fixtures = array(
        'app.note',
        'app.version',
        'app.corp',
        'app.advertisement',
        'app.member',
        'app.member_detail',
        'app.member_newsletter',
        'app.groups_members_join',
        'app.group',
        'app.job',
        'app.job_category',
        'app.jobs_categories_join',
        );
    function startTest() {
        $this->Note =& ClassRegistry::init('Note');
    }
    function testNoteInstance() {
        $this->assertTrue(is_a($this->Note, 'Note'));
    }
    function testNoteFind() {
        $this->Note->recursive = -1;
        $results = $this->Note->find('first');
        $this->assertTrue(!empty($results));
        $expected = array('Note' => array(
            'id' => 1,
            'model' => 'Lorem ipsum dolor sit amet',
            'model_id' => 1,
            'created' => '2010-03-30 16:26:59',
            'member_id' => 1,
            'body' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida,phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam,vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit,feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.',
            'is_active' => 1
        ));
        $this->assertEqual($results, $expected);
    }
    function testJobWithNotes() {
        $this->Job =& ClassRegistry::init('Job');
        $uniqueValue = rand(0,time());
        $savedJob = $this->Job->saveAll(array(
            'Job' => array(
                'title' => "Test Job [$uniqueValue] from ".__FILE__,
            ),
            'Note' => array(
                array(
                    'model' => "Job",
                    'body' => "Here is a unique value [$uniqueValue]")
            )));
        $this->assertTrue($savedJob);
        $this->assertEqual($this->Note->find('first',array(
            'fields' => array('body'),
            'conditions' => array('model_id' => $this->Job->id, 'model' => $this->Job->name))),
            array('Note'=>array('body'=>"Here is a unique value [$uniqueValue]")));
    }
}
?>

如果你想要它,这里是

<强> note_fixture.php

<?php 
class NoteFixture extends CakeTestFixture {
    var $name = 'Note';
    var $fields = array(
        'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
        'model' => array('type'=>'string', 'null' => false, 'default' => NULL, 'length' => 32),
        'model_id' => array('type'=>'integer', 'null' => false, 'default' => NULL),
        'created' => array('type'=>'datetime', 'null' => false, 'default' => NULL),
        'member_id' => array('type'=>'integer', 'null' => false, 'default' => NULL),
        'body' => array('type'=>'text', 'null' => false, 'default' => NULL),
        'is_active' => array('type'=>'boolean', 'null' => false, 'default' => '1'),
        'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
    );
    var $records = array(array(
        'id' => 1,
        'model' => 'Lorem ipsum dolor sit amet',
        'model_id' => 1,
        'created' => '2010-03-30 16:26:59',
        'member_id' => 1,
        'body' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida,phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam,vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit,feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.',
        'is_active' => 1
    ));
}
?>