So I'm having trouble with unit testing CakePHP models. Simple stuff like writing tests that my validation rules are catching errors etc.
To begin with, I have a model called NewsItem. Its defined in my MySQL database using the following schema
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
);
The model is following
<?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'
)
)
);
}
?>
And in my test case I have
// Validation lets All data good through
function testValidationAllowsNormalData()
{
$this->assertTrue($this->NewsItem->save(array('NewsItem' => array('title' => 'A news item', 'body' => 'Some news'))));
}
However I'm my test case fails. Any ideas, suggestions, comments?