在测试中无法访问 Yii 单元测试装置
config/test.php
'components'=>array(
'fixture'=>array(
'class'=>'system.test.CDbFxtureManager'
),
),
测试/单元/EntityTest.php(扩展CDbTestCase)
public $fixtures = array('entities'=>'Entity'),
测试/装置/Entity.php
return array(
'entity1'=>array('slug'=>'slug1', 'title'=>'title1'),
'entity2'=>array('slug'=>'slug2', 'title'=>'title2'),
);
现在,在EntityTest类中,我尝试获取我的实体
$entities = $this->entities;
$entity = $this->entities('entity1');
输出是“类“EntityTest”的未知属性“实体””。测试类为“Entity”,数据库中的表名为“tbl_entity”,“CDbConnection”组件的“tablePrefix”选项设置为“tbl_”
config/test.php
'components'=>array(
'fixture'=>array(
'class'=>'system.test.CDbFxtureManager'
),
),
tests/unit/EntityTest.php (extends CDbTestCase)
public $fixtures = array('entities'=>'Entity'),
tests/fixtures/Entity.php
return array(
'entity1'=>array('slug'=>'slug1', 'title'=>'title1'),
'entity2'=>array('slug'=>'slug2', 'title'=>'title2'),
);
Now, in EntityTest class I try to get my entities
$entities = $this->entities;
$entity = $this->entities('entity1');
Output is 'Unknown property "entities" for class "EntityTest"'. Testing class is 'Entity', table name in database is 'tbl_entity', 'tablePrefix' option of 'CDbConnection' component is set to 'tbl_'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们去尸检吧! :)
你的问题是固定装置中文件的命名。您提到它是一个
tests/fixtures/Entity.php
,它应该位于您的模型、类的名称之后,但实际上,fixtures< 中的文件/code> 文件夹应以模型的
tableName
命名。我相信您的Entity
模型的真实表名称是entity
(全部小写),因此只需重命名该文件即可。我多次遇到类似的问题,文档中清楚地说明了解决方案: "A Fixture 表示为 PHP脚本名称(不带后缀)与表名相同(如果需要模式名称,则应在表名前面添加前缀)。"Let's go necroposting! :)
Your problem is the naming of the file in fixtures. You mention that it's a
tests/fixtures/Entity.php
, which should be after the name of your Model, class, but in fact, files infixtures
folder should be named aftertableName
s of your Models. I believe the real table name for yourEntity
model isentity
(all in lower case), so just rename the file. I had the similar problem several times, and the solution is clearly stated in the documentation: "A fixture is represented as a PHP script whose name (without suffix) is the same as the table name (if schema name is needed, it should be prefixed to the table name)."如果您使用的是 setUp() 方法,也可以尝试此方法。当您使用自己的方法重新定义它时,只需调用parent::setUp()即可。
Also try this if you are using setUp() method. Just call parent::setUp(), when you are redefining it with your own method.
看看这个解决方案 Yii Fixtures Issue? 我也遇到了同样的问题。我忽略了要扩展的类,请确保扩展 CDbTestCase。
Take a look on this solution Yii Fixtures Issue? I also came up with the same problem. I overlook the class to extend, make sure to extend the CDbTestCase.