2012-07-29 55 views
3

我遵循“yii 1.1和php5的敏捷web应用程序开发”一书,并且我正在使用fixtures部分进行测试。我跟着他们的代码,但我不能访问到灯具...Yii Fixtures ---例外:类'ProjectTest'的未知属性'项目'

我跑我的测试使用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 \ protected \ tests \ unit \ ProjectTest.php

我把$ this-> projects('project1')(从书中)换成$ th is-> projects ['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 \保护\ CONFIG \ 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', 
     ), 
    ), 
) 
); 

回答

0

我发现错误...

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

夹具需要“S”

希望能帮到别人

1

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

我只是想对此发表评论:

我改变$这个 - >项目( 'PROJECT1')(从书)为$ this-> projects ['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