CakePHP - 未调用单元测试验证
我正在做一些单元测试,但遇到模型验证未调用的问题。
这是我在夹具中的内容。
<?php
class ItemFixture extends CakeTestFixture {
var $name = 'Item';
var $import = array(
'model' => 'Item',
'table' => 'items'
);
var $records = array(array(
'id' => 1,
'title' => 'Title2',
'year' => '1992'
),array(
'id' => 2,
'title' => 'Lorem ipsum dolor sit amet',
'year' => '1995'
));
}
?>
这是我在模型单元测试中得到的内容
<?php
App::import('Model', 'Item');
class ItemTest extends Item {
var $name = 'ItemTest';
var $useTable = 'items';
var $useDbConfig = 'test';
}
class ItemTestCase extends CakeTestCase {
var $fixtures = array('app.item');
function startTest() {
$this->ItemTest =& ClassRegistry::init('ItemTest');
}
function testAdd() {
$data = array(
'ItemTest' => array(
'title' => 'Best article Evar!'
)
);
$result = $this->ItemTest->saveAll($data);
}
function endTest() {
}
}
?>
这是我在实际模型中进行的验证。
'year' => array(
'numeric' => array(
'rule' => 'numeric',
'allowEmpty' => false,
'message' => 'Numbers only'
),
'minLength' => array(
'rule' => array('minLength', 4),
'message' => 'Year in YYYY format'
),
'maxLength' => array(
'rule' => array('maxLength', 4),
'message' => 'Year in YYYY format'
)
看,在 testAdd 中,我没有通过年份,它不应该为空,但它实际上已经过去了。为什么没有调用验证?
谢谢你,
蒂伊
I'm doing some unit testing but having issues with Model validation not getting called.
Here is what I have in the Fixture.
<?php
class ItemFixture extends CakeTestFixture {
var $name = 'Item';
var $import = array(
'model' => 'Item',
'table' => 'items'
);
var $records = array(array(
'id' => 1,
'title' => 'Title2',
'year' => '1992'
),array(
'id' => 2,
'title' => 'Lorem ipsum dolor sit amet',
'year' => '1995'
));
}
?>
Here is what I have in the model unit test
<?php
App::import('Model', 'Item');
class ItemTest extends Item {
var $name = 'ItemTest';
var $useTable = 'items';
var $useDbConfig = 'test';
}
class ItemTestCase extends CakeTestCase {
var $fixtures = array('app.item');
function startTest() {
$this->ItemTest =& ClassRegistry::init('ItemTest');
}
function testAdd() {
$data = array(
'ItemTest' => array(
'title' => 'Best article Evar!'
)
);
$result = $this->ItemTest->saveAll($data);
}
function endTest() {
}
}
?>
And here is the validation that I have in the actual Model.
'year' => array(
'numeric' => array(
'rule' => 'numeric',
'allowEmpty' => false,
'message' => 'Numbers only'
),
'minLength' => array(
'rule' => array('minLength', 4),
'message' => 'Year in YYYY format'
),
'maxLength' => array(
'rule' => array('maxLength', 4),
'message' => 'Year in YYYY format'
)
See, in testAdd, I'm not passing year which is not supposed to be empty, but it actually passed. Why isn't the validation getting called?
Thank you,
Tee
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
allowEmpty
仅在数据包含指定字段(在您的情况下为year
)时才有效。要使验证失败,您必须添加'year' => ''
到testAdd
中的$data
数组或添加'required' =>; true
到您的验证规则以强制数据中存在year
字段。另请参阅http://book.cakephp.org/view/1148/allowEmpty
allowEmpty
only works if the data contains the specified field, in your caseyear
. To make the validation fail you either have to add'year' => ''
to your$data
array intestAdd
or add'required' => true
to your validation rule to enforce there is ayear
field in the data.See also http://book.cakephp.org/view/1148/allowEmpty