所以我正在使用 CakePHP v1.2.5。在我当前的项目中,我决定在编写功能代码时开始编写测试(耶 TDD)。我在加载夹具时遇到问题。
为了帮助这个过程,我将描述我的代码(现在真的很简单)。我的模型是这样定义的
// app/models/newsitem.php
<?php
class NewsItem extends AppModel
{
var $name='NewsItem';
}
?>
// app/tests/fixtures/newsitem_fixture.php
<?php
class NewsItemFixture extends CakeTestFixture
{
var $name = 'NewsItem';
var $import = 'NewsItem';
var $records = array(
array('id' => '1', 'title' => 'News Item 1', 'body' => 'This is the first piece of news', 'created' => '2007-03-18 10:39:23', 'modified' => '2007-03-18 10:41:31'),
array('id' => '2', 'title' => 'News 2', 'body' => 'This is some other piece of news', 'created' => '2009-05-04 9:00:00', 'modified' => '2009-05-05 12:34:56')
);
}
?>
// app/tests/models/newsitem.test.php
<?php
App::Import('Model', 'NewsItem');
class NewsItemTestCase extends CakeTestCase
{
var $fixtures = array('app.newsitem');
function setUp()
{
$this->NewsItem =& ClassRegistry::init('NewsItem');
}
function testFindAll()
{
$results = $this->NewsItem->findAll();
$expected = array(
array('NewsItem' => array('id' => '1', 'title' => 'News Item 1', 'body' => 'This is the first piece of news', 'created' => '2007-03-18 10:39:23', 'modified' => '2007-03-18 10:41:31')),
array('NewsItem' => array('id' => '2', 'title' => 'News 2', 'body' => 'This is some other piece of news', 'created' => '2009-05-04 9:00:00', 'modified' => '2009-05-05 12:34:56'))
);
print_r($results);
$this->assertEqual($results, $expected);
}
}
?>
无论如何,我的问题是,当我在浏览器中运行测试套件(转到http://localhost/test.php)时,测试用例运行器会尝试加载我的应用程序的布局(这很奇怪,因为我只是在测试model) 它引用了另一个显然没有加载到测试数据库中的模型,我得到一个错误。
如果我
var $fixtures = array('app.newsitem')
从我的 NewsItemTestCase 文件中删除该行,测试用例会正常运行,但它不会加载固定装置(原因很明显)。
有什么想法、建议吗?老实说,我很难找到超过 3 个关于这个问题的教程。