【发布时间】:2010-12-26 17:07:33
【问题描述】:
所以我在对 CakePHP 模型进行单元测试时遇到了麻烦。简单的事情,比如编写我的验证规则正在捕获错误的测试等。
首先,我有一个名为 NewsItem 的模型。它使用以下架构在我的 MySQL 数据库中定义
CREATE TABLE news_items (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(140) NOT NULL,
body TEXT NOT NULL,
modified DATETIME DEFAULT NULL,
created DATETIME DEFAULT NULL
);
模型正在跟随
<?php
class NewsItem extends AppModel {
var $name = 'NewsItem';
var $validate = array(
'title' => array(
'titleRule-1' => array(
'rule' => array('maxLength', 140),
'message' => 'News item\'s title\'s length exceeds limit of 140 characters'
),
'titleRule-2' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Cannot save news item without a title'
)
)
);
}
?>
在我的测试用例中,我有
// Validation lets All data good through
function testValidationAllowsNormalData()
{
$this->assertTrue($this->NewsItem->save(array('NewsItem' => array('title' => 'A news item', 'body' => 'Some news'))));
}
但是我的测试用例失败了。有什么想法、建议、cmets?
【问题讨论】:
标签: php unit-testing testing cakephp