unit-testing - cakephp 单元测试模型,fixtures 问题

标签 unit-testing cakephp tdd cakephp-1.2

所以我正在使用 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 ),测试用例运行程序会尝试加载我的应用程序的布局(这很奇怪,因为我只是在测试模型)引用了另一个显然没有加载到测试数据库中的模型,我得到了一个错误。

如果我删除 var $fixtures = array('app.newsitem') 我的 NewsItemTestCase 文件中的行,测试用例运行正常,但它没有加载装置(原因很明显)。

有什么想法、建议吗?老实说,我很难找到超过 3 个关于这个问题的教程。

最佳答案

这是很久以前的事了,但问题在于命名约定,如果夹具名为“NewsItemFixture”,则文件应该是 news_item_fixture,而不是 newsitem_fixture。如果您想要名为 newsitem_fixture 的文件,fixture 类应该是 NewsitemFixture。

同样适用于所有其他文件,例如您那里的模型。

关于unit-testing - cakephp 单元测试模型,fixtures 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1885233/

相关文章:

java - 有条件地忽略 JUnit 4 中的测试

php - 有什么简单的方法可以将 Hoptoad 与 CakePHP 一起使用?

unit-testing - Clojure 中的单元测试局部函数(letfn)?

ruby-on-rails - 如何在 Rails 3 的测试设置中设置 session 哈希?

Cakephp URL 重写和重定向

tdd - 测试正确的东西,避免重复覆盖的技术

python-3.x - Python 测试对实例化为另一个类中的变量的类方法的调用

unit-testing - 良好的双重比较容忍度

java - 使用 TestNG 注释进行测试

mysql - CAKEPHP 外键约束 - 调试显示正确的值