Yii Fixtures Issue?

左心房为你撑大大i 提交于 2019-12-23 17:43:54

问题


I seem to be having a problem with fixtures in Yii. The problem seems to be the following,

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

The model Project exists and I have the fixtures in a file name tbl_project.php in the fixtures folder of tests and my table name is called tbl_project. Inside the fixtures file is the following.

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' => '',
         ),
    );       

This is actually from the book "Agile Web Application Development with Yii". When I run the test case I get the following with no test result information.

PHPUnit 3.6.10 by Sebastian Bergmann.

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

If I remove the fixtures array from the top I get the following.

Time: 0 seconds, Memory: 9.25Mb

There was 1 error:

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

Which obviously makes sense. I dont know what Im doing wrong.


回答1:


  1. Make sure the test class looks like this

    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. Check the fixture configuration in protected/config/test.php ... Should look like ...


    ...
    '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. Ultimately check the permissions on the fixture file making sure it can be read



回答2:


Also be sure you are calling parents setUp() method in your own setUp()

class SomeTest extends CDbTestCase {

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

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

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

}



回答3:


Is your test class deriven from CDbTestCase instead of CTestCase?

your Test Class should look something like this:

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



回答4:


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',
);

}



回答5:


make sure that setup come with its parent

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


来源:https://stackoverflow.com/questions/9886947/yii-fixtures-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!