我正在关注“使用yii 1.1和php5进行敏捷Web应用程序开发”一书,我正在使用fixtures部分进行测试。我按照他们的代码但我无法访问夹具......
我正在使用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 \ protected \ tests \ fixtures \ 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' =>'',
),
),
?>
我的Project测试类: C:\ wamp \ www \ agileBook \ protected \ tests \ unit \ ProjectTest.php
我为$ this-&gt; projects ['project1']更改了$ this-&gt; 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 \ protected \ 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 :(得分:1)
如上所述,$ fixture必须更正为$ fixtures,
我只是想对此发表评论:
我为$ this-&gt; projects ['project1']更改了$ this-&gt; 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');
答案 1 :(得分:0)
我发现了错误......
public $fixtures = array('projects'=>'Project');
灯具需要“S”
希望有人帮助