我正在关注“使用yii 1.1和php5进行敏捷Web应用程序开发”一书,我正在使用fixtures部分进行测试。我按照他们的代码但我无法访问夹具......
在使用PHPunit配置fixture之后,我在第6章运行我的单元测试,它返回给我
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$
这是我的ProjectTest.php文件
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);
}
}
有人可以帮忙吗?
答案 0 :(得分:1)
检查你是否在pŕotected/ tests中引导文件指向正确的路径并检查同一文件夹中的WebTastCase.php文件是否正在向右倾斜'TEST_BASE_URL',如:
define('TEST_BASE_URL','http://localhost/my_app/index-test.php/');
并尝试从protected / tests文件夹中运行phpunit
答案 1 :(得分:0)
在你的框架目录中应该有一个名为test的目录,其中应该有CDbTestCase.php。你可以验证一下吗? 如果它丢失了你就发现了问题,否则你的代码无法到达框架目录。
答案 2 :(得分:0)
使用PHPStorm时遇到了同样的问题。它在我站在目录'unit'上运行测试(ctrl + shift + F10)时有效,但如果我站在测试文件本身上则无法工作。