Yii灯具问题?

时间:2012-03-27 09:39:41

标签: php testing yii phpunit

我似乎遇到了Yii中的灯具问题。问题似乎如下,

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

模型项目存在,我在测试的fixtures文件夹中有文件名tbl_project.php的夹具,我的表名称为tbl_project。夹具文件内部如下。

return array(
        'project1' => array(
            'name' => 'Test Project 1',
            'description' => 'This is test project 1',
            'create_time' => '',
            'create_user_id' => '',
            'update_time' => '',
            'update_user_id' => '',
         ),
        'project2' => array(
            'name' => 'Test Project 2',
            'description' => 'This is test project 2',
            'create_time' => '',
            'create_user_id' => '',
            'update_time' => '',
            'update_user_id' => '',
         ),
        'project3' => array(
            'name' => 'Test Project 3',
            'description' => 'This is test project 3',
            'create_time' => '',
            'create_user_id' => '',
            'update_time' => '',
            'update_user_id' => '',
         ),
    );       

这实际上来自“使用Yii进行敏捷Web应用程序开发”一书。当我运行测试用例时,我得到以下内容而没有测试结果信息。

PHPUnit 3.6.10 by Sebastian Bergmann.

Configuration read from ETC/protected/tests/phpunit.xml

如果我从顶部移除灯具阵列,我会得到以下内容。

Time: 0 seconds, Memory: 9.25Mb

There was 1 error:

1) ProjectTest::testRead
Exception: Unknown method 'projects' for class 'ProjectTest'. 

这显然是有道理的。我不知道我做错了什么。

5 个答案:

答案 0 :(得分:4)

  1. 确保测试类看起来像这样
  2. 
        class ProjectTest extends CDbTestCase{
    
        protected $fixtures = array(
                    'projects' => 'Project',
                );
    
        public function testRead(){
    
            $receivedProject = $this->projects('Project1');
            $this->assertTrue($receivedProject instanceof Project);
            $this->assertEquals($receivedProject->name,'test 1');
        }
        ...`
    
    1. 检查protected / config / test.php中的夹具配置......应该看起来像......
    2. 
      
          ...
          'components'=>array(
                  'fixture'=>array(
                      'class'=>'system.test.CDbFixtureManager',
                  ),
                              'db'=>array(
                                  'connectionString' =>'mysql:host=localhost;dbname=your_db_name',
                                  'emulatePrepare' => true,
                                  'username' => 'username',
                                  'password' => 'passwd',
                                  'charset' => 'utf8',
                              ),
          ....
      
      1. 最终检查灯具文件的权限,确保可以阅读

答案 1 :(得分:3)

还要确保在自己的 setUp()

中调用父 setUp()方法
class SomeTest extends CDbTestCase {

    public $fixtures = array(
        'somes' => 'Some',
    );

    protected function setUp() {
        parent::setUp();
        // your code....
    }

    // your tests .......................

}

答案 2 :(得分:1)

您的测试类是否来自CDbTestCase而不是CTestCase?

你的测试类应该是这样的:

class ProjectTest extends CDbTestCase{

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

public function testRead(){

    $receivedProject = $this->projects('Project1');
    $this->assertTrue($receivedProject instanceof Project);
    $this->assertEquals($receivedProject->name,'test 1');
}

答案 3 :(得分:0)

class ProjectTest extends CDbTestCase
{

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

public function testGetUserOptions()
{
    $project = $this->projects('project1');
    $options = $project->userOptions;
    $this->assertTrue(is_array($options));
    $this->assertTrue(count($options) > 0);

}
public $fixtures=array(
'projects'=>'Project',
'users'=>'User',
'projUsrAssign'=>':tbl_project_user_assignment',
);

}

答案 4 :(得分:0)

确保设置附带其父

<i>public function setUp() {
    parent::setUp();
}</i>