在测试中无法访问 Yii 单元测试装置

发布于 2024-12-27 10:42:46 字数 728 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扮仙女 2025-01-03 10:42:46

我们去尸检吧! :)

你的问题是固定装置中文件的命名。您提到它是一个 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 in fixtures folder should be named after tableNames of your Models. I believe the real table name for your Entity model is entity (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)."

温柔嚣张 2025-01-03 10:42:46

如果您使用的是 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.

花伊自在美 2025-01-03 10:42:46
$entity = $this->entities['entity1']; // returns an array
$entity = $this->entities('entity1'); // returns an active record
$entity = $this->entities['entity1']; // returns an array
$entity = $this->entities('entity1'); // returns an active record
南巷近海 2025-01-03 10:42:46

看看这个解决方案 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文