在cakePhp中创建多个fixture [英] Creating multiple fixtures in cakePhp
问题描述
我一直在尝试,但是徒劳地,在cakePhP中创建多个夹具。目前,我需要测试一个需要两个不同的fixture数据库表的函数。如果可能,有人可以发布一些简单的代码段,并显示我创建多个灯具。
这里有一个简单的夹具: app / tests / fixtures / entity_fixtures.php
在您的情况下,您将创建两个这些文件,每个模型一个和不同的名称。
<?php
EntityFixture类扩展CakeTestFixture {
var $ name ='Entity';
var $ table ='entities';
//定义字段
var $ fields = array(
'entity_id'=> array('type'=>'integer','key'=> ;'primary'),
'type'=> array('type'=>'string','length'=> 255),
'created'=> ,
'modified'=>''datetime'
);
var $ records = array(
array('entity_id'=> 1,'type'=>'entity','created'=>'2010-01-01 ','modified'=>'2010-01-01')
);
}
?>
在测试用例中包含测试用例顶部的fixtures
class EntityTestCase extends CakeTestCase {
var $ fixtures = array(
'plugin.myplugin.entity',/ /包含一个样例插件夹具
'entity',//上面代码所示的夹具
'blogs'//包含第三个夹具(应该在app / tests / fixtures / blog_fixture.php中)
);
}
I've been trying, but in vain, to create multiple fixtures in cakePhP. Currently, I need to test a function that requires two different fixture database tables. If possible, could someone please post some simple snippet and show me create multiple fixtures.
Here's a simple fixture: app/tests/fixtures/entity_fixtures.php In your case you will create two of these files, one per model and with different names.
<?php
class EntityFixture extends CakeTestFixture {
var $name = 'Entity';
var $table = 'entities';
// Define the fields
var $fields = array(
'entity_id' => array('type' => 'integer', 'key' => 'primary'),
'type' => array('type' => 'string' , 'length' => 255),
'created' => 'datetime',
'modified' => 'datetime'
);
var $records = array(
array('entity_id' => 1, 'type' => 'entity', 'created'=>'2010-01-01', 'modified' => '2010-01-01')
);
}
?>
When in your test case include your fixtures at the top of your test case
class EntityTestCase extends CakeTestCase {
var $fixtures = array(
'plugin.myplugin.entity', // Including a sample plugin fixture
'entity', // The fixture above shown in code above
'blogs' // Including a third fixture (should be in app/tests/fixtures/blog_fixture.php)
);
}
这篇关于在cakePhp中创建多个fixture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!