Fatal error: Class 'CDbTestCase' not found

2020-05-01 23:47发布

问题:

I'm following the "Agile web application development with yii 1.1 and php5" book and i'm at the testing with fixtures section. I followed their code but i can't access to the fixture...

I'm running my unit test at chapter 6 after configuring fixture with PHPunit and it returns me this

Last login: Sat Oct  6 20:09:36 on ttys000
xyz-MacBook-Pro:~ inganious$ /usr/local/bin/phpunit/phpunit /Applications/MAMP/htdocs/trackstar/protected/tests/unit/ProjectTest.php

Fatal error: Class 'CDbTestCase' not found in /Applications/MAMP/htdocs/trackstar/protected/tests/unit/ProjectTest.php on line 3
xyz-MacBook-Pro:~ inganious$ 

Here is my ProjectTest.php file

class ProjectTest extends CDbTestCase
{

    public $fixtures=array
    (
            'projects'=>'Project',
    );

    public function testCreate()
      {
        //CREATE a new Project
        $newProject=new Project;
        $newProjectName = 'Test Project Creation';
        $newProject->setAttributes(array(
        'name' => $newProjectName,
        'description' => 'This is a test for new project creation',
        'createTime' => '2009-09-09 00:00:00',
        'createUser' => '1',
        'updateTime' => '2009-09-09 00:00:00',
        'updateUser' => '1',
        )
      );
        $this->assertTrue($newProject->save(false));
        //READ back the newly created Project to ensure the creation worked

        $retrievedProject=Project::model()->findByPk($newProject->id);
        $this->assertTrue($retrievedProject instanceof Project);
        $this->assertEquals($newProjectName,$retrievedProject->name);
      }
        public function testRead()
      {
        $retrievedProject = $this->projects('project1');
        $this->assertTrue($retrievedProject instanceof Project);
        $this->assertEquals('Test Project 1',$retrievedProject->name);
      }
        public function testUpdate()
      {
        $project = $this->projects('project2');
        $updatedProjectName = 'Updated Test Project 2';
        $project->name = $updatedProjectName;
        $this->assertTrue($project->save(false));
        //read back the record again to ensure the update worked
        $updatedProject=Project::model()->findByPk($project->id);
        $this->assertTrue($updatedProject instanceof Project);
        $this->assertEquals($updatedProjectName,$updatedProject->name);
      }
        public function testDelete()
      {
        $project = $this->projects('project2');
        $savedProjectId = $project->id;
        $this->assertTrue($project->delete());
        $deletedProject=Project::model()->findByPk($savedProjectId);
        $this->assertEquals(NULL,$deletedProject);
    }
}

Can anyone help please ?

回答1:

check if you bootstrap file in pŕotected/tests is pointing to the right paths and check if the WebTastCase.php file in the same folder is poiting to the right 'TEST_BASE_URL', something like:

define('TEST_BASE_URL','http://localhost/my_app/index-test.php/');

and try to run phpunit from inside the protected/tests folder



回答2:

In your framework directory there should be a dir named test and within it there should be CDbTestCase.php. can you verify this please? if it's missing you've found the problem, else your code cannot reach the framework dir.



回答3:

I encountered the same problem while using PHPStorm. It works when I run the tests (ctrl+shift+F10) while standing on the directory 'unit', but will not work if I stand on the test file itself.