Can anyone help me with what assertions are required in a basic save method test case in CakePHP 2.0?
I have Product, User and News model, I am looking to write a test case for the submit method in the News model and there are so many ways/things to include I was just wondering what is actually needed and what isn’t. I have basic fixtures setup for all models.
The method I’m testing will effectively be this:
class News extends AppModel {
public submit($productId, $userId, $newsData) {
// Logic which checks for user and products existence, saves and returns submitted row
}
}
Test Case
public function testSubmit() {
// Save News
$newsData = array(
'News' => array(
'title' => 'Here is the title of the news',
'body' => 'Here is the news body',
'source' => 'News came from here'
)
);
$news = $this->News->submit('product-1', 'user-1', $newsData);
// Now what?
}
Simply assert that $news is an array, object, that the array is equal to an array you expect… Whatever your method returns, you should know it even before implementing the method (test driven development) and be able to assert the result using one or more than one of the phpunit assert methods.
Like simply $this->assertTrue($news); Check the manual for all the asserts. http://www.phpunit.de/manual/current/en/
Also look at the CakePHP core tests to get an idea of how to do test.
Or look at some open source plugin exmaples like
https://github.com/CakeDC/tags/blob/2.0/Test/Case/Model/TaggedTest.php
or
https://github.com/CakeDC/users/blob/2.0/Test/Case/Model/UserTest.php