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

后端 未结 3 2111
说谎
说谎 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 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

    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

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

提交回复
热议问题