CakePHP Test Fixtures Drop My Tables Permanently After Running A Test Case

后端 未结 3 2055
说谎
说谎 2021-01-27 03:28

I\'m not sure what I\'ve done wrong in my CakePHP unit test configuration. Every time I run a test case, the model tables associated with my fixtures are missing form my test da

相关标签:
3条回答
  • 2021-01-27 03:55

    in your CakeTestCase class, just put a member 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';
      }
    
    0 讨论(0)
  • 2021-01-27 04:00

    I think that's the idea. Fixtures are fixed tables with fixed contents. They're not the tables you use otherwise in production or during development. That's why they have a separate connection in database.php, you're supposed to use a different database or at least a different prefix for test tables. These will be created based on your fixtures before each test and torn down afterwards, so you're always testing against a known set of data.

    0 讨论(0)
  • 2021-01-27 04:01

    Your database config looks right...

    You shouldn't have to do

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

    My Recommendation

    You should use the cake bake shell to create the models for all of your models and let it overwrite them (obviously back up any custom models first). You don't need to go through the validation and association prompts (say "n"), but yes to all overwrites (again, backup your code first).

    When you do that, it also creates a basic test case and a fixture for the model.

    Use the test case it creates, and add to it...

    What you need is to then call into being EVERY single fixture you use and all associated fixtures... all the hasMany and belongsTo and HABTM associations need the fixture loaded, but they don't automatically get called into being in the test suite.

    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]")));
        }
    }
    ?>
    

    And just in case you want it, here is

    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
        ));
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题