Yii的赛程 - 例外:未知属性“项目”上课“ProjectTest”(Yii Fixtures —

2019-09-20 09:48发布

我继“敏捷Web应用开发与谊1.1和PHP5”的书,我在用夹具部分进行测试。 我跟着他们的代码,但我不能访问到灯具...

我跑我的测试使用PHPUnit并返回我这个

c:\wamp\www\agileBook\protected\tests>phpunit unit/ProjectTest.php
PHPUnit 3.6.11 by Sebastian Bergmann.

Configuration read from C:\wamp\www\agileBook\protected\tests\phpunit.xml

←[31;1mE←[0m

Time: 0 seconds, Memory: 5.75Mb

There was 1 error:

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

C:\wamp\yii\framework\test\CDbTestCase.php:63
C:\wamp\www\agileBook\protected\tests\unit\ProjectTest.php:11
C:\wamp\bin\php\php5.3.13\phpunit:46

←[37;41m←[2KFAILURES!
←[0m←[37;41m←[2KTests: 1, Assertions: 0, Errors: 1.
←[0m←[2K

我怎样才能使它发挥作用?

谢谢您的帮助

我的灯具:C:\ WAMP \ WWW \ agileBook \保护\测试\灯具\ tbl_project.php

<?php 

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

),

?>

我的项目测试类:C:\ WAMP \ WWW \ agileBook \保护\测试\单位\ ProjectTest.php

我这 - >项目(“PROJECT1”)(从书)公司招聘$>项目[“PROJECT1”]改变$,因为我在一个论坛帖子投射是一个数组,而不是一个方法见。

<?php

class ProjectTest extends CDbTestCase{

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

    public function testRead(){
    // READ the new project
        $receivedProject = $this->projects['project1'];
        $this->assertTrue($receivedProject instanceof Project);
        $this->assertEquals($receivedProject->name,'Test Project 1');

    }

}

?>

我的测试配置:C:\ WAMP \ WWW \ agileBook \保护\设置\ test.php的

<?php

return CMap::mergeArray(
require(dirname(__FILE__).'/main.php'),
array(
    'components'=>array(
        'fixture'=>array(
            'class'=>'system.test.CDbFixtureManager',
        ),
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=trackstar_test',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ),
    ),
)
);

Answer 1:

如前所述,$灯具必须修正到$灯具,

我只是想对此发表评论:

我这 - >项目(“PROJECT1”)(从书)公司招聘$>项目[“PROJECT1”]改变$,因为我在一个论坛帖子投射是一个数组,而不是一个方法见。

如果你这样做,上述试验将失败。 从Yii的文档的原因:

    // return all rows in the 'Comment' fixture table
    $comments = $this->comments; 
    // return the row whose alias is 'sample1' in the `Post` fixture table
    $post = $this->posts['sample1'];
    // return the AR instance representing the 'sample1' fixture data row
    $post = $this->posts('sample1');

http://www.yiiframework.com/doc/guide/1.1/en/test.unit



Answer 2:

我发现错误...

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

夹具需要一个“S”

希望它可以帮助别人



文章来源: Yii Fixtures — Exception: Unknown property 'projects' for class 'ProjectTest'