CakePHP测试装置在运行测试用例后永久删除我的表

CakePHP测试装置在运行测试用例后永久删除我的表,cakephp,testing,Cakephp,Testing,我不确定我在CakePHP单元测试配置中做错了什么。每次我运行一个测试用例时,我的测试数据库中都会丢失与我的装置相关联的模型表 运行单个测试用例后,我必须使用phpMyAdmin重新导入数据库表 以下是相关文件: 这是我试图测试comment.php的类。此表在测试后被丢弃 database.php mycomment.test.php文件。这张桌子老是掉下来 我已经处理这个问题一天了,我开始被CakePHP的单元测试所拖累。除了这个问题,Servral times现在我已经在运行测试后将数据插

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

运行单个测试用例后,我必须使用phpMyAdmin重新导入数据库表

以下是相关文件:

这是我试图测试comment.php的类。此表在测试后被丢弃

database.php

mycomment.test.php文件。这张桌子老是掉下来


我已经处理这个问题一天了,我开始被CakePHP的单元测试所拖累。除了这个问题,Servral times现在我已经在运行测试后将数据插入到“默认”数据库配置中了!我的配置怎么了

我想这就是我的想法。固定装置是具有固定内容的固定桌子。它们不是您在生产或开发过程中使用的表。这就是为什么它们在database.php中有一个单独的连接,您应该使用不同的数据库,或者至少为测试表使用不同的前缀。在每次测试之前,将根据您的装置创建这些装置,然后将其拆下,因此您总是根据已知的数据集进行测试。

我认为这就是想法。固定装置是具有固定内容的固定桌子。它们不是您在生产或开发过程中使用的表。这就是为什么它们在database.php中有一个单独的连接,您应该使用不同的数据库,或者至少为测试表使用不同的前缀。这些将在每次测试之前根据您的装置创建,并在测试之后拆除,因此您总是根据一组已知的数据进行测试。

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

你不该这么做

$this->Comment->useDbConfig = 'test_suite';
我的推荐

您应该使用shell为您的所有模型创建模型,并让它覆盖这些模型,然后首先备份任何自定义模型。您不需要执行验证和关联提示,提示为n,但如果要再次覆盖所有内容,请先备份代码

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

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

您需要的是调用您使用的每一个装置和所有相关装置。。。所有hasMany和belongsTo和HABTM关联都需要加载fixture,但它们不会自动在测试套件中被调用

note.test.php

为了以防万一你想要它,这是

注意:fixture.php


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

你不该这么做

$this->Comment->useDbConfig = 'test_suite';
我的推荐

您应该使用shell为您的所有模型创建模型,并让它覆盖这些模型,然后首先备份任何自定义模型。您不需要执行验证和关联提示,提示为n,但如果要再次覆盖所有内容,请先备份代码

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

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

您需要的是调用您使用的每一个装置和所有相关装置。。。所有hasMany和belongsTo和HABTM关联都需要加载fixture,但它们不会自动在测试套件中被调用

note.test.php

为了以防万一你想要它,这是

注意:fixture.php


在CaketTestCase类中,只需在dropTables中放置一个成员:


在CaketTestCase类中,只需在dropTables中放置一个成员:


那么,不可能运行一个测试用例,例如CommentTestCase,然后立即再次运行CommentTestCase?我只是尝试使用CakePHP test.php页面,以便能够在不重新导入整个数据库的情况下多次运行CommentTestCase。@Frank如果您在Fixture中提供数据,您可以在一行中运行任意多次。测试用例并不意味着只使用数据库中的数据运行,数据库设置和拆卸是测试运行的一部分。@Frank您可以告诉夹具从现有表中获取数据,但它仍然会设置一个单独的表,至少您应该让它这样做,并在最后将其拆下。我希望你已经读过手册了?是的,我已经阅读了手册的测试部分好几次,试图弄清楚到底发生了什么。您提到:只要您在fixture中提供数据。在我的测试用例中,我不是在夹具中而是在测试方法中生成数据。我猜这是不正确的,它必须在fixture中?@Frank为了分离,我认为您应该使用fixture来生成数据。我想,在生成数据的情况下,这并没有太大的区别。关键是,如果您使用的是fixture,那么它们会在测试结束时拆下您的测试表。我对蛋糕测试的细节了解不够
s告诉你是否或如何放弃使用固定装置。但是,由于您可能没有做任何特殊的工作,您应该将您的方法放入已建立的框架中,并在单独的表中使用fixture。因此,不可能运行测试用例,例如CommentTestCase,然后立即再次运行CommentTestCase?我只是尝试使用CakePHP test.php页面,以便能够在不重新导入整个数据库的情况下多次运行CommentTestCase。@Frank如果您在Fixture中提供数据,您可以在一行中运行任意多次。测试用例并不意味着只使用数据库中的数据运行,数据库设置和拆卸是测试运行的一部分。@Frank您可以告诉夹具从现有表中获取数据,但它仍然会设置一个单独的表,至少您应该让它这样做,并在最后将其拆下。我希望你已经读过手册了?是的,我已经阅读了手册的测试部分好几次,试图弄清楚到底发生了什么。您提到:只要您在fixture中提供数据。在我的测试用例中,我不是在夹具中而是在测试方法中生成数据。我猜这是不正确的,它必须在fixture中?@Frank为了分离,我认为您应该使用fixture来生成数据。我想,在生成数据的情况下,这并没有太大的区别。关键是,如果您使用的是fixture,那么它们会在测试结束时拆下您的测试表。我对蛋糕测试的细节了解不够,无法告诉你是否可以或如何放弃使用固定装置。由于您可能没有做任何特殊的事情,因此您应该将您的方法放入已建立的框架中,并在单独的表中使用fixture。我有$droptables=false,但我的表仍然每次都被删除。我有$droptables=false,但我的表仍然每次都被删除。
<?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';
  }
  <?php
   class CommentFixture extends CakeTestFixture
   {
     var $name = "Comment";

     var $import = 'Comment';
   }
   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);
}
$this->Comment->useDbConfig = 'test_suite';
<?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]")));
    }
}
?>
<?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
    ));
}
?>
<?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';
  }